views:

833

answers:

4

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?

+14  A: 

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.

Mike Caron
+2  A: 

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.

htw
+1  A: 

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.

Rose Perrone
A: 

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

bonjing