(!object)和(object == nil)有什么区别吗? [英] is there a difference between (!object) and (object == nil)?

查看:79
本文介绍了(!object)和(object == nil)有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
ObjectiveC:if(obj){…} AND if( obj!= 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 type int. The expression !E is equivalent to (0==E).

由于nil为0,因此最后一句话完全适用.

Since nil is 0, that last sentence applies exactly.

这篇关于(!object)和(object == nil)有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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