如何检查无 [英] How to check for Nil
问题描述
我目前正在学习Objective-C和基本的C编程语言.
I am currently studying objective-c and the basic c programming language.
我对特定代码行有疑问:
I have a questions about a particular line of code:
if (!balance)
平衡是创建的对象.我知道这段代码正在检查对象平衡是否为零,这是正确的吗?
Balance is an object that is created. I understand that this code is checking to see if the object balance is nil or not, is this correct?
有人可以解释一下代码到底如何检查nil吗?如果balance的值非零,是否返回0;如果balance的值为0,是否返回1?
Could somebody please explain how exactly the code checks for nil? Does it return 0 if the value of balance is nonzero and 1 if the value is 0?
提前谢谢.
推荐答案
在Objective-C中,nil
大致类似于0
,NULL
或false
,但是用于对象指针.在if
语句中,其行为与上述标量值之一相同.例如,以下两个if
语句应产生相同的结果:
In Objective-C, nil
is roughly analogous to 0
, NULL
or false
, but for object pointers. In an if
statement, it will behave the same as one of the aforementioned scalar values. For example, the following two if
statements should produce the same results:
NSNumber *balance = nil;
if (!balance) {
// do something if balance is nil
}
if (balance == nil) {
// do something if balance is nil
}
这篇关于如何检查无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!