(!object)和(object == nil)有什么区别吗? [英] is there a difference between (!object) and (object == nil)?
问题描述
Possible Duplicate:
ObjectiveC: if (obj) {…} AND if (obj != nil) {…}, which is better?
这两个条件之间是否有区别:
Is there a difference between these two conditions:
if (!object) {
// do something
}
和. . .
if (object == nil) {
// do something
}
推荐答案
不,没有区别.这两个例子都完全相同.从C规范 6.5.3.3一元算术运算符:
No, there is no difference. Both of those examples are exactly the same. From the C spec 6.5.3.3 Unary arithmetic operators:
如果逻辑运算符
!
的结果不等于0,则结果为0;如果逻辑运算符的值等于0,则结果为1. strong>int
.表达式!E
等同于(0==E)
.
The result of the logical negation operator
!
is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has typeint
. The expression!E
is equivalent to(0==E)
.
由于nil
为0,因此最后一句话完全适用.
Since nil
is 0, that last sentence applies exactly.
这篇关于(!object)和(object == nil)有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!