objective-c中有命名初始化方法的约定吗?

时间:2023-01-15 15:13:15

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

在可以通过不同的init初始化的objective-c类中…方法,将所有初始化程序都通用的初始化代码收集到一个通用方法中是常识,该方法从其他init*方法(有时也从awakeFromNib)调用。

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

该如何命名这个方法有约定吗?初始化?initCommon吗?……?

4 个解决方案

#1


5  

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.

根据Apple的说法,初始化方法应该总是以“init”开头,然后是描述参数的名称组件。如果一个类有多个初始化器,那么应该将这些方法链接在一起,这样它们中只有一个在做所有的工作,而其他的则应该简单地为丢失的参数提供默认值。

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

例如,Person类可能有如下的init。方法:

- (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].

正如@dreamlax所指出的,苹果的文档建议(当使用ARC编译时,编译器需要)用调用[super init]的返回值重新分配self。

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.

文档还建议在执行任何进一步的初始化之前检查nil,因为如果对[super init]的调用返回nil,那么在调用返回时self就已经被释放了,所以不再需要初始化实例。

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:

苹果的文档还建议避免在init中调用访问器方法……方法;相反,他们建议直接设置实例变量。因此,initWithFirstName:lastName:age:上面显示的方法应该以类似于以下示例的方式编写:

- (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


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. 指定的初始化器应该有详细的文档说明,任何子类化您的代码的人都需要知道他们需要使用[super init…]调用哪个初始化器。
  3. The designated initializer is usually the initializer with the most arguments... not always, though, so be careful.
  4. 指定的初始化器通常是具有最多参数的初始化器……但并非总是如此,所以要小心。
  5. Only invoke [super init] from within the designated initializer, all other init methods should somehow call that initializer.
  6. 只有在指定的初始化器中调用[super init],所有其他的初始化方法都应该以某种方式调用该初始化器。

#3


1  

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

听起来您正在描述指定的初始化器。它没有与其他初始化器不同的特殊命名约定。通常,初始化器以“init”开头。

#4


0  

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

我通常从基本的- (id)init方法初始化所有内容,并从其他init方法调用该方法:

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

#1


5  

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.

根据Apple的说法,初始化方法应该总是以“init”开头,然后是描述参数的名称组件。如果一个类有多个初始化器,那么应该将这些方法链接在一起,这样它们中只有一个在做所有的工作,而其他的则应该简单地为丢失的参数提供默认值。

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

例如,Person类可能有如下的init。方法:

- (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].

正如@dreamlax所指出的,苹果的文档建议(当使用ARC编译时,编译器需要)用调用[super init]的返回值重新分配self。

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.

文档还建议在执行任何进一步的初始化之前检查nil,因为如果对[super init]的调用返回nil,那么在调用返回时self就已经被释放了,所以不再需要初始化实例。

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:

苹果的文档还建议避免在init中调用访问器方法……方法;相反,他们建议直接设置实例变量。因此,initWithFirstName:lastName:age:上面显示的方法应该以类似于以下示例的方式编写:

- (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


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. 指定的初始化器应该有详细的文档说明,任何子类化您的代码的人都需要知道他们需要使用[super init…]调用哪个初始化器。
  3. The designated initializer is usually the initializer with the most arguments... not always, though, so be careful.
  4. 指定的初始化器通常是具有最多参数的初始化器……但并非总是如此,所以要小心。
  5. Only invoke [super init] from within the designated initializer, all other init methods should somehow call that initializer.
  6. 只有在指定的初始化器中调用[super init],所有其他的初始化方法都应该以某种方式调用该初始化器。

#3


1  

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

听起来您正在描述指定的初始化器。它没有与其他初始化器不同的特殊命名约定。通常,初始化器以“init”开头。

#4


0  

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

我通常从基本的- (id)init方法初始化所有内容,并从其他init方法调用该方法:

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