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).
NSNull *nullValue = [NSNull null]; |
NSArray *arrayWithNull = [NSArray arrayWithObject:nullValue]; |
NSLog(@"arrayWithNull: %@", arrayWithNull); |
// output: "arrayWithNull: (<null>)" |
It is important to appreciate that the NSNull instance is semantically different from NO or falseâÂÂthese both represent a logical value; the NSNull instance represents the absence of a value. The NSNull instance is semantically equivalent to nil, however it is also important to appreciate that it is not equal to nil. To test for a null object value, you must therefore make a direct object comparison.
id aValue = [arrayWithNull objectAtIndex:0]; |
if (aValue == nil) { |
NSLog(@"equals nil"); |
} |
else if (aValue == [NSNull null]) { |
NSLog(@"equals NSNull instance"); |
if ([aValue isEqual:nil]) { |
NSLog(@"isEqual:nil"); |
} |
} |
} |
// output: "equals NSNull instance" |
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)