在objective-c中是否有命名初始化方法的约定? [英] Is there a convention for naming initializer method in objective-c?

查看:108
本文介绍了在objective-c中是否有命名初始化方法的约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可以通过不同init ...方法初始化的目标c类中,收集初始化代码是常见的,这些初始化代码对于所有初始化器是通用的,它们是从其他init *方法调用的也有时来自awakeFromNib)。

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

有没有一个如何命名这个方法的约定? initializer initCommon ? ...?

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

推荐答案

根据Apple,初始化方法应始终以init开头,描述参数。如果一个类有多个初始化器,那么这些方法应该链接在一起,这样只有其中一个方法可以完成所有的工作,其他方法应该为缺少的参数提供默认值。

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.

例如,Person类可能有以下init ...方法:

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

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

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

文档还建议检查 nil 在执行任何进一步的初始化之前,因为如果调用 [super init] 返回 nil 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.

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

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

这篇关于在objective-c中是否有命名初始化方法的约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆