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? ...?
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;
}
This method is called the "designated initializer". Apple has some pretty good documentation on it, to summarize, though:
[super init...]
[super init]
from within the designated initializer, all other init methods should somehow call that initializer.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With