views:

36

answers:

1

Recently I realized I needed to add an argument to the init method for a helper class I've got. The helper class deals with alert views so it already has a bunch of arguments in the init, which are looked at, tweaked, and then sent on to the alert view.

Since I'm using the method as it is in various places, I don't want to risk crashing (by missing one of those places and getting an 'unrecognized selector' in the hands of a customer) so I decided to add a second init method.

I.e.

- (id)initWithA:B:C:D:

and

- (id)initWithA:B:C:foo:D:

Right now I've simply copy pasted the first one's implementation into the foo: one, but ideally what would be nice is making the first call the second, i.e.

- (id)initWithA:a B:b C:c D:d
{
    return [self initWithA:a B:b C:c foo:nil D:d];
}

but I'm not sure if this is acceptable or not. Code appears to be working fine.

+3  A: 

Yes, that is perfectly acceptable and actually quite common.

This is why we have things called a "Designated Initializer". That's the initializer method to which all other initializers get redirected (usually).

Dave DeLong
Wow, definitely need to search more before I ask something. Thanks a lot! Have to wait 10 mins before I can accept apparently. Will accept soonish.
Kalle
@Kalle theoretically all the answers are in the documentation. However it's still (usually) worth asking the question just so that other people can find the information more easily in the future. :)
Dave DeLong