[NSNull null]
and nil
are not equivalent. [NSNull null]
is meant to represent the concept of NULL (as in no object) in cases where nil
cannot be used, for example in an NSArray
(as you can only insert objects in them). [NSNull null]
is an object (always the same object), while nil
is a pointer to 0.
NSHipster has a nice discussion here. He says:
NSNull is used throughout Foundation and other frameworks to skirt around the limitations of collections like NSArray and NSDictionary not being able to contain nil values. You can think of NSNull as effectively boxing the NULL or nil value so that it can be used in collections.
If you have:
NSString *aString;
if ( aString == (NSString *)[NSNull null] )
{
// Logic handling
}
then something's wrong, aString
should point to an NSString
object (or subclass), or nil
. But not [NSNull null]
which is an object of a different class, you shouldn't cast from one to the other.
EDIT:
Given in the comments you state that you wish to check if the string is empty (as in @""
), that is different. See this question. An empty string is an NSString
object, it is not nil
and it is not [NSNull null]
.