如何检查无 [英] How to check for Nil

查看:84
本文介绍了如何检查无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习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大致类似于0NULLfalse,但是用于对象指针.在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屋!

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