开发者

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

开发者 https://www.devze.com 2022-12-19 18:52 出处:网络
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 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? initCom开发者_JAVA技巧mon? ...?


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:

  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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号