简体   繁体   English

iPhone - 当[super init]失败时使用self = [super init]

[英]iPhone - Use of self = [super init] when [super init] fails

What is the difference beetween : 有什么区别:

// 1
-(id) init {
    self = [super init];
    if (self) {
        ... do init ....
    }
    return self;
}

// 2 - I guess it's exactly the same as previous
-(id) init {
    if (self = [super init]) {
        ... do init ....
    }
    return self;
}

// 3 - is this one a really bad idea and if yes, Why ?
-(id) init {
    self = [super init];
    if (!self) return nil;

    ... do init ....

    return self;
}

// 4 - I think this one sounds OK, no ? But what is returned... nil ?
-(id) init {
    self = [super init];
    if (!self) return self;

    ... do init ....

    return self;
}

EDIT : Added thanks to Peter M. 编辑:感谢Peter M.

// 5 - I really like the readability of this one
-(id) init {
    if(!(self = [super init])) return self;  // or nil

    ... do init ....

    return self;
}

they all do the same thing but first one are commonly used because apple suggested 他们都做同样的事情,但第一个是常用的,因为苹果建议

the second one was commonly used but it will introduce compiler warning on new version of Xcode so apple decided to change it to first one 第二个是常用的,但它会在新版本的Xcode上引入编译器警告,因此苹果决定将其改为第一个

  1. The common one used by Apple Apple常用的一种
  2. Does the same but the compiler does not know if you didn't mean == instead of = . 是否相同但编译器不知道你是不是指==而不是= You can block the warning by using another (...) around the expression, but it's not easy to read. 您可以通过在表达式周围使用另一个(...)来阻止警告,但这并不容易阅读。
  3. This one would be preferred by any good coding standards (usually not as a one-liner but with bracers). 任何良好的编码标准(通常不是单线,而是带有护腕)都是首选。 When your initialization code is long, this one significantly increase readibility. 当您的初始化代码很长时,这会显着提高可读性。
  4. The same as 3 but you lose some readibility. 与3相同,但你失去了一些可读性。 Returning nil makes the code easier to understand (maybe this is only my opinion, someone else can say 4 is better than 3). 返回nil使代码更容易理解(也许这只是我的意见,其他人可以说4比3更好)。

In summary: use 1 or 3. When initialization code is long, you should use 3 to avoid having most of method code in one if block. 总结:使用1或3.当初始化代码很长时,你应该使用3来避免在一个if块中使用大多数方法代码。 Apple uses only 1 but don't follow it blindly. Apple仅使用1但不盲目追随。 Apple has no documented coding standards and sometimes what they recommend is very questionable. Apple没有记录编码标准,有时他们推荐的是非常值得怀疑的。

You can use 4 instead of 3 but don't mix 3 & 4 in your code. 您可以使用4而不是3,但不要在代码中混用3和4。

1 is suggested simply because it's clear. 建议1只是因为它很清楚。

2 works, but is just bad practice. 2作品,但只是不好的做法。 Don't embed logic in your conditionals if you don't have to. 如果你不需要,不要在你的条件中嵌入逻辑。

3 makes my head hurt. 3让我头疼。 Typically, you want to avoid negative checks if you can. 通常,如果可以,您希望避免否定检查。

4 same as 3. 4与3相同。

Both 1 and 2 are easier to read than 3 and 4, also because they are the one's used in Apple's code; 1和2都比3和4更容易阅读,也因为它们是Apple代码中使用的那个; many developers are used to 1 and 2. 许多开发人员习惯于1和2。

Number 4 and 3 are the same, as if !self evaluates as true, then it means self is nil , so the statement return self is equivelant to return nil . 数字4和3是相同的,好像!self评估为true,那么它意味着selfnil ,所以语句return self是equivelant return nil Some people will say you shouldn't have multiple exit points (returns) in a method, but in reality having multiple return statements can cut down the number of if statements (which reduce the readability) 有些人会说你不应该在方法中有多个退出点(返回),但实际上有多个return语句可以减少if语句的数量(这会降低可读性)

Number 2 has been common, however in the latest versions of xcode you will get a compiler warning if and if statement contains a single = (as many times it is a typo, when you intended to use == to compare BOOL values). 数字2已经很常见了,但是在xcode的最新版本中,你会得到一个编译器警告if和if语句是否包含单个= (当你打算使用==来比较BOOL值时,这是很多次它是一个错字)。 To silence this warning you have to surround the statement with brackets, so instead of if (foo = bar) you would have if ((foo = bar)) 为了使这个警告保持沉默,你必须用括号括起语句,所以不是if (foo = bar)而是if ((foo = bar))
But Apple must have realised that Number 2 is used a lot, and they added an exception to the rule, so you using it will now not cause a compiler warning. 但Apple必须意识到Number 2已被大量使用,并且他们在规则中添加了一个例外,所以你使用它现在不会引起编译器警告。

As Number 2 is an exception to the rule, you shouldn't really use it. 由于Number 2是规则的例外,因此您不应该使用它。 So that makes Number 1 the preferred method. 这使得Number 1成为首选方法。

To add to the fray .. I prefer: 加入战斗..我更喜欢:

if(!(self = [super init]) ) return self;

Keeping it all on the one line, and no warnings in XCode 4.2 :D 将所有内容保留在一行上,并且在XCode 4.2中没有警告:D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 if(self = [super init])与if((self = [super init])) - if (self = [super init]) vs. if ((self = [super init])) InitWithCoder,[super init]还是[super initWithCoder]? - InitWithCoder, [super init] or [super initWithCoder]? 当我初始化自定义单元格时,它没有设置为'[(super或self)init ...]'的结果时返回'self' - Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell if(self = [super init]) - LLVM警告! 你是怎么处理它的? - if(self = [super init]) - LLVM warning! How are you dealing with it? 谁能给我一个关于“ self = super init”的简单解释? - Can anyone give me an easy explanation about “self=super init”? 在我的自定义alloc方法中未将其设置为'[((super或self)init…])的结果时返回'self' - Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method “self”时使用的实例变量未通过Analyzer设置为“[(super或self)init ...]”的结果 - Instance variable used while 'self' is not set to the result of '[(super or self) init…]' via Analyzer 超级初始化精灵与位置不匹配 - super init Sprites doesn't match position 调用[[super allocWithZone:nil] init],消息传递机制 - Calling [[super allocWithZone:nil] init], messaging mechanism 覆盖子类中的init方法并调用[super initWith:bla],正确的方法? - Overriding the init method in a subclass and calling [super initWith:bla], correct way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM