[英]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上引入编译器警告,因此苹果决定将其改为第一个
==
instead of =
. ==
而不是=
。 You can block the warning by using another (...)
around the expression, but it's not easy to read. (...)
来阻止警告,但这并不容易阅读。 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,那么它意味着self
是nil
,所以语句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.