objective-c [NSNull null] 和 nil 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/836601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between [NSNull null] and nil?
提问by Thanks
Here's a context where I have seen that:
这是我看到的上下文:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
why not nil in that place?
为什么在那个地方不为零?
回答by Mike Caron
Directly from Apple:
直接来自苹果:
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).
NSNull 类定义了一个单例对象,用于在 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.
所以在你的例子中,这正是正在发生的事情,程序员选择将一个空对象放入控制器数组中,其中 nil 不允许作为值。
回答by hbw
You cannot add a nilvalue to an NSArrayor NSMutableArray. If you need to store a nilvalue, you need to use the NSNullwrapper class, as shown in that snippet you have. This is specified in the documentation.
您不能nil向NSArray或 中添加值NSMutableArray。如果需要存储nil值,则需要使用NSNull包装类,如您拥有的代码段所示。这是在文档中指定的。
回答by Rose Perrone
Collection classes like NSArrayand NSDictionarycannot contain nilvalues. NSNULLwas created specifically as a placeholder for nil. It can be put into collection classes, and only takes up space.
集合类喜欢NSArray并且NSDictionary不能包含nil值。NSNULL是专门作为nil. 可以放入集合类,只占空间。
NSNulldefines 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定义了一个单例对象,这意味着只有一个 NSNull 实例(您使用 来创建[NSNull null]),但它可以在任意多的地方使用。
回答by Hampden123
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.
我们都同意 [NSNull null] 作为需要对象的占位符很有用,如上所述。但是除非它明确用于您的对象的赋值,否则不应将其用于比较,这是我过去犯过的错误。
id a;
NSLog(@"Case 1");
if (a == nil) NSLog(@"a == nil");
if (a == Nil) NSLog(@"a == Nil");
if ([a isEqual:[NSNull null]]) NSLog(@"a isEqual:[NSNull null]");
NSLog(@"Case 2");
a = [NSNull null];
if (a == nil) NSLog(@"a == nil");
if (a == Nil) NSLog(@"a == Nil");
if ([a isEqual:[NSNull null]]) NSLog(@"a isEqual:[NSNull null]");
Output:
输出:
2014-01-31 10:57:11.179 MCDocsApp[13266:a0b] Case 1
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.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] 案例 2
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] a isEqual:[NSNull null]
2014-01-31 10:57:11.180 MCDocsApp[13266:a0b] a isEqual:[NSNull null]
回答by Suraj K Thomas
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).
NSNull 类定义了一个单例对象,用于在 nil 被禁止作为值的情况下(通常在集合对象,例如数组或字典中)表示空值。
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.
您不能向 NSArray 或 NSMutableArray 添加 nil 值。如果需要存储一个 nil 值,则需要使用 NSNull 包装类。
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.
像 NSArray 和 NSDictionary 这样的集合类不能包含 nil 值。NSNULL 是专门作为 nil 的占位符创建的。可以放入集合类,只占空间。
See the link for reference
请参阅链接以供参考
回答by bonjing
nil marks the end of an array after an array of objects...
nil 在对象数组之后标记数组的结尾...

