Adding a custom initWith?

You can create one designated initializer that accepts all parameters that you want to make available in initialization.

Then you call from your other -(id)init your designated initializer with proper parameters.

Only the designated initializer will initialize super class [super init].

Example:

- (id)init
{
    return [self initWithX:defaultX andY:defaultY];
}

- (id)initWithPosition:(NSPoint)position
{
    return [self initWithX:position.x andY:position.y];
}


- (id)initWithX:(int)inPosX andY:(int)inPosY
{
    self = [super init];
    if(self) {
        NSLog(@"_init: %@", self);
        posX = inPosX;
        posY = inPosY;
    }
    return self;
}

The designated initializer is -(id)initWithX:andY: and you call it from other initializers.

In case you want to extend this class you call your designated initializer from subclass.

Leave a Comment