iPhone SDK : Please explain why "nil ==" is different than "== nil"

标签 iphone c objective-c null

我已经编写 iPhone SDK 大约 6 个月了,但对一些事情有点困惑......我想问其中一个:

为什么以下被认为是不同的?

if (varOrObject == nil)
{

}

对比

if (nil == varOrObject)
{

}

来自 perl 背景,这让我感到困惑......

如果这两个例程一个接一个地放在代码中,有人可以解释为什么两者之一(第二个)是正确的而第一个不是。 varOrObject 在两个 if 语句之间不会发生变化。

没有具体的代码发生这种情况,只是我在很多地方读到这两个语句不同,但不知道为什么。

提前致谢。

最佳答案

它们是一样的。你看过的大概是说如果把==写错了=,前者会把值赋给变量,而if条件将为假,而后者将是编译时错误:

if (variable = nil) // assigns nil to variable, not an error.

if (nil = variable) // compile time error

后者使您有机会在编译时更正错误。

关于iPhone SDK : Please explain why "nil ==" is different than "== nil",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3076501/

相关文章:

iphone - 如何暂停/播放 NSTimer?

iphone - 文件缓存问题

c - 在 c 和 bash 之间传递值

c - VPATH 构建中未考虑 EXTRA_DIST

objective-c - 时间四舍五入到最接近的第 10 分钟

iphone - 如何备份/同步我在documents目录中拥有的sqlite数据库? iCloud是答案吗?

ios - 将 ARSCNView 场景背景内容设置为 iPhone X 相机

c - 打印后随机打印字符 'result'

ios - 显示 Entypo 字体中的 5 位基本 Unicode 字符

ios - 在按钮操作上实例化 UINavigationController