[NSNull null] 和 nil 有什么区别?
这是我看到的上下文:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
为什么不在那个地方为零?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
直接来自 Apple:
因此,在您的示例中,这正是发生的情况,程序员选择将 null 对象放入控制器数组中,其中不允许 nil 作为值。
Directly from Apple:
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.
您无法将
nil
值添加到NSArray
或NSMutableArray
。 如果您需要存储nil
值,则需要使用NSNull
包装类,如您的代码片段所示。 这是在 文档。You cannot add a
nil
value to anNSArray
orNSMutableArray
. If you need to store anil
value, you need to use theNSNull
wrapper class, as shown in that snippet you have. This is specified in the documentation.我们都同意 [NSNull null] 作为需要对象的占位符很有用,如上所述。 但除非它在对象的赋值中明确使用,否则不应该在比较中使用它,这是我过去犯过的错误。
输出:
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] 案例 1
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] a == nil
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] a == Nil
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] 案例 2
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] a isEqual:[NSNull null ]
We all agree that [NSNull null] is useful as a placeholder where an object is required, as elaborated above. But unless it's explicitly used in assignment for your object, it should not be used in comparison, a mistake I have made in the past.
Output:
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] Case 1
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] a == nil
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] a == Nil
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] Case 2
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] a isEqual:[NSNull null]
NSArray
和NSDictionary
等集合类不能包含nil
值。NSNULL
是专门为nil
创建的占位符。 可以放入集合类中,并且只占用空间。NSNull
定义了一个单例对象,这意味着只有一个 NSNull 实例(您使用[NSNull null]
创建),但它可以在任意多个实例中使用随心所欲的地方。Collection classes like
NSArray
andNSDictionary
cannot containnil
values.NSNULL
was created specifically as a placeholder fornil
. It can be put into collection classes, and only takes up space.NSNull
defines a singleton object, which means that there's only ever a single instance of NSNull (which you create using[NSNull null]
), but it can be used in as many places as you wish.NSNull 类定义了一个单例对象,用于在禁止将 nil 作为值的情况下(通常在数组或字典等集合对象中)表示 null 值。
您不能将 nil 值添加到 NSArray 或 NSMutableArray。 如果需要存储 nil 值,则需要使用 NSNull 包装类。
像 NSArray 和 NSDictionary 这样的集合类不能包含 nil 值。 NSNULL 是专门为 nil 创建的占位符。 可以放入集合类中,并且只占用空间。
请参阅参考链接
https://developer .apple.com/library/content/documentation/Cocoa/Conceptual/NumbersandValues/Articles/Null.html
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).
You cannot add a nil value to an NSArray or NSMutableArray. If you need to store a nil value, you need to use the NSNull wrapper class.
Collection classes like NSArray and NSDictionary cannot contain nil values. NSNULL was created specifically as a placeholder for nil. It can be put into collection classes, and only takes up space.
See the link for reference
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/NumbersandValues/Articles/Null.html
nil 在对象数组之后标记数组的结尾...
nil marks the end of an array after an array of objects...