Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a convention for naming initializer method in objective-c?

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? ...?

like image 705
VoidPointer Avatar asked Feb 28 '23 14:02

VoidPointer


2 Answers

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;
}

UPDATE

As @dreamlax points out, Apple's documentation recommends (and when compiling with ARC, the compiler requires) reassigning self with the return value from the call to [super init].

The docs also recommend checking for nil before performing any further initialization That's because if the call to [super init] returns nil, self would already have been deallocated by the time the call returns, so there would no longer be an instance to initialize.

Apple's documentation also suggests avoiding calls to accessor methods in init... methods; instead, they recommend directly setting the instance variables. So the initWithFirstName:lastName:age: method shown above should ideally be written in a manner similar to the following example:

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

    if (self == nil) return nil;

    _firstName = [firstName copy];
    _self.lastName = [lastName copy];
    _age = [age copy];

    return self;
}
like image 58
jlehr Avatar answered Apr 07 '23 13:04

jlehr


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.
like image 20
kubi Avatar answered Apr 07 '23 14:04

kubi