views:

69

answers:

4

In an objective-c class that can be initialized through different init... methods, it is common sense to collect initialization code that is common to all initializers into one common method that is called from the other init* methods (or also sometimes from awakeFromNib).

Is there a convention for how this method should be named? initializer? initCommon? ...?

+1  A: 

It sounds like you are describing the designated initializer. It does not have a special naming convention that is different from other initializers. Typically, initializers start with "init".

gerry3
A: 

I typically initialize everything from the basic - (id)init method, and call that one from the other init methods:

- (id)init
{
    if( self = [super init] )
    {
        myValue = 0.0f;
        myOtherValue = 0.0f;
    }
    return self;
}

- (id)initWithValue:(float)value
{
    if( self = [self init] ) // Calling the other init method (not [super init])
    {
        myValue = value;
    }
    return self;
}
Martin Cote
This is the opposite of the way you should be doing it. The assumption is that the longest initializer is the designated initializer. If someone subclasses your Class, and overwrites your `-(id)init` method, they will likely call `[super initWithValue:x]`, ensuring that any code you put in `-init` never gets called.
kubi
If your class is available for people to subclass then simply document that the `init` method shouldn't be overridden unless it calls `[super init]`. There should be no assumption.
dreamlax
@Martin Cote: Also, keep in mind that all instance variables are zero'd out when your object is allocated, so assigning 0 or `nil` to any instance variables is unnecessary.
dreamlax
@kubi, Interesting point. Is this documented somewhere?
Martin Cote
@martin http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-106376As far as I know it's a pretty standard convention.
kubi
+4  A: 

According to Apple, initializer methods should always begin with the word 'init,' followed by name components that describe the arguments. If a class has more than one initializer, the methods should be chained together so that only one of them is doing all the work, and the others should simply provide default values for the missing arguments.

So for example, a Person class might have the following init... methods:

- (id)init
{
    return [self initWithFirstName:nil
                          lastName:nil];
}

- (id)initWithFirstName:(NSString *)firstName
               lastName:(NSString *)lastName
{
    return [self initWithFirstName:firstName
                          lastName:lastName
                               age:nil];
}

- (id)initWithFirstName:(NSString *)firstName
               lastName:(NSString *)lastName
                    age:(NSNumber *)age
{
    [super init];

    self.firstName = firstName;
    self.lastName = lastName;
    self.age = age;

    return self;
}
jlehr
The Apple docs also recommend you assign the return value of `[super init]` to `self` and to ensure that `self` is not nil. I also vaguely remember something about not using accessor methods from within `init` methods.
dreamlax
+2  A: 

This method is called the "designated initializer". Apple has some pretty good documentation on it, to summarize, though:

  1. The designated initializer should be well documented, anyone who subclasses your code needs to know which initializer they need to call using [super init...]
  2. The designated initializer is usually the initializer with the most arguments... not always, though, so be careful.
  3. Only invoke [super init] from within the designated initializer, all other init methods should somehow call that initializer.
kubi