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

好久不见. 提交于 2020-01-04 04:22:07

问题


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:


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



回答2:


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.



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/2247960/is-there-a-convention-for-naming-initializer-method-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!