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?
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.
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.
A singleton object used to represent null values in collection objects that don't allow nil values.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With