Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 820
Thanks Avatar asked May 07 '09 19:05

Thanks


People also ask

Is nil same as?

Nil means the same as zero. It is usually used to say what the score is in sports such as rugby or football. They beat us one-nil in the final. If you say that something is nil, you mean that it does not exist at all.

What is nil in Swift?

In Swift, nil isn't a pointer—it's the absence of a value of a certain type. Optionals of any type can be set to nil , not just object types.

What is NSNull?

A singleton object used to represent null values in collection objects that don't allow nil values.

Does NULL exist in Swift?

However there is another data type in Swift called Optional, whose default value is a null value ( nil ). You can use optional when you want a variable or constant contain no value in it. An optional type may contain a value or absent a value (a null value).


2 Answers

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.

like image 76
Mike Caron Avatar answered Sep 19 '22 11:09

Mike Caron


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.

like image 30
hbw Avatar answered Sep 19 '22 11:09

hbw