init应该在self init上分配,还是测试相等? [英] Should init be assigning, or testing equality, on self init?

查看:117
本文介绍了init应该在self init上分配,还是测试相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 https://github.com/facebook/facebook-ios-sdk .git ,并注意到代码中有两个这样的结构:

 (id)init {
if((self == [super init])){
...
}
}

我希望 self 被分配到这里,没有测试平等:



{pre> (id)init {
if((self = [super init])){
...
}

或至少:

 (id)init {
self = [super init];
if(self){
...
}
}

这是在 src / FBDialog.m sample / Hackbook / Hackbook / DataSet.m 之内。



(抱歉,如果这应该在错误跟踪器;无法找到实际提交的任何方法...: - /

解决方案

应该分配自己,因为 [super init] 可能会返回与self不同的对象。 p>

通常正常检查是否正常工作,但不需要工作,您应该始终分配



我相信,在这种特殊情况下使用 == 时,最近版本的cl ang甚至会发出警告。


I cloned from https://github.com/facebook/facebook-ios-sdk.git today, and noticed two spots in the code which have this construct:

(id) init {
    if ((self == [super init])) {
        ...
    }
}

I would expect self to be assigned here, not tested for equality:

(id) init {
    if ((self = [super init])) {
        ...
    }
}

or at the very least:

(id) init {
    self = [super init];
    if (self) {
        ...
    }
}

This is within src/FBDialog.m and sample/Hackbook/Hackbook/DataSet.m.

(Sorry if this should be in the bug tracker instead; couldn't find a way to actually submit anything there... :-/

解决方案

It should assign self, because [super init] may return a different object than self.

Checking for equality will often work but isn't required to work. You should always assign.

I believe that recent versions of clang even emit warnings when using == in this particular case.

这篇关于init应该在self init上分配,还是测试相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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