What's the difference between [NSNull null] and nil?
Asked Answered
D

6

51

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?

Duenna answered 7/5, 2009 at 19:39 Comment(1)
Doesn't get any better than this ---> nshipster.com/nilPhysician
G
87

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).

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.

Gorga answered 7/5, 2009 at 19:43 Comment(0)
E
14

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, as shown in that snippet you have. This is specified in the documentation.

Eraser answered 7/5, 2009 at 19:46 Comment(0)
P
11

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.

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] 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]

Puberty answered 31/1, 2014 at 16:6 Comment(2)
-[NSObject isEqual:] is the correct and recommended method to compare an objc object with NSNull.Platino
Newly-alloc’d NSObjects start life with their contents set to 0. This means that all pointers that object has to other objects begin as nil, so it’s unnecessary to, for instance, set self.(association) = nil in init methods. In case 1, "a" has default value 0 which indicates nil/Nil and for second case you assigns singleton object used to represent null, that is why it outputs null not nil.Ruphina
M
9

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.

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.

Milquetoast answered 3/7, 2010 at 5:3 Comment(0)
R
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 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

Raker answered 15/3, 2018 at 11:44 Comment(0)
C
-15

nil marks the end of an array after an array of objects...

Currie answered 12/8, 2010 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.