[ACCEPTED]-What's the difference between [NSNull null] and nil?-nsnull

Accepted answer
Score: 86

Directly from Apple:

The NSNull class defines 8 a singleton object you use to represent 7 null values in situations where nil is prohibited 6 as a value (typically in a collection object 5 such as an array or a dictionary).

So in 4 your example, that's exactly what's happening, the 3 programmer is choosing to put a null object 2 into the controllers array, where nil is 1 not allowed as a value.

Score: 14

You cannot add a nil value to an NSArray or NSMutableArray. If you 3 need to store a nil value, you need to use 2 the NSNull wrapper class, as shown in that snippet 1 you have. This is specified in the documentation.

Score: 11

We all agree that [NSNull null] is useful 11 as a placeholder where an object is required, as 10 elaborated above. But unless it's explicitly 9 used in assignment for your object, it should 8 not be used in comparison, a mistake I have 7 made in the past.

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 6 MCDocsApp[13266:a0b] Case 1

2014-01-31 10:57:11.179 5 MCDocsApp[13266:a0b] a == nil

2014-01-31 4 10:57:11.179 MCDocsApp[13266:a0b] a == Nil

2014-01-31 3 10:57:11.180 MCDocsApp[13266:a0b] Case 2

2014-01-31 2 10:57:11.180 MCDocsApp[13266:a0b] a isEqual:[NSNull 1 null]

Score: 9

Collection classes like NSArray and NSDictionary cannot contain 5 nil values. NSNULL was created specifically as a placeholder for nil. It can be put into collection classes, and only takes up space.

NSNull defines 4 a singleton object, which means that there's 3 only ever a single instance of NSNull (which 2 you create using [NSNull null]), but it can be used in 1 as many places as you wish.

Score: 0

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 7 or NSMutableArray. If you need to store 6 a nil value, you need to use the NSNull 5 wrapper class.

Collection classes like NSArray 4 and NSDictionary cannot contain nil values. NSNULL 3 was created specifically as a placeholder 2 for nil. It can be put into collection classes, and 1 only takes up space.

See the link for reference

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/NumbersandValues/Articles/Null.html

More Related questions