[NSNull null] 和 nil 有什么区别? [英] What's the difference between [NSNull null] and nil?
本文介绍了[NSNull null] 和 nil 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我看到的上下文:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
为什么在那个地方不为零?
why not nil in that place?
推荐答案
直接来自 苹果:
NSNull 类定义了一个单例对象,用于在禁止将 nil 用作值的情况下(通常在集合对象,例如数组或字典中)表示空值.
The NSNull class defines a singleton object you use to represent null values in situations where nil is prohibited as a value (typically in a collection object such as an array or a dictionary).
因此,在您的示例中,这正是正在发生的事情,程序员选择将一个空对象放入控制器数组中,其中不允许将 nil 作为值.
So in your example, that's exactly what's happening, the programmer is choosing to put a null object into the controllers array, where nil is not allowed as a value.
这篇关于[NSNull null] 和 nil 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文