There are two situations:
It is possible that an object is [NSNull null]
, or it is impossible.
Your application usually shouldn't use [NSNull null];
you only use it if you want to put a "null" object into an array, or use it as a dictionary value. And then you should know which arrays or dictionaries might contain null values, and which might not.
If you think that an array never contains [NSNull null]
values, then don't check for it. If there is an [NSNull null]
, you might get an exception but that is fine: Objective-C exceptions indicate programming errors. And you have a programming error that needs fixing by changing some code.
If an object could be [NSNull null]
, then you check for this quite simply by testing
(object == [NSNull null])
. Calling isEqual
or checking the class of the object is nonsense. There is only one [NSNull null]
object, and the plain old C operator checks for it just fine in the most straightforward and most efficient way.
If you check an NSString
object that cannot be [NSNull null]
(because you know it cannot be [NSNull null]
or because you just checked that it is different from [NSNull null]
, then you need to ask yourself how you want to treat an empty string, that is one with length 0. If you treat it is a null
string like nil
, then test (object.length == 0)
. object.length will return 0 if object == nil
, so this test covers nil objects and strings with length 0. If you treat a string of length 0 different from a nil string, just check if object == nil
.
Finally, if you want to add a string to an array or a dictionary, and the string could be nil, you have the choice of not adding it, replacing it with @""
, or replacing it with [NSNull null]
. Replacing it with @""
means you lose the ability to distinguish between "no string" and "string of length 0". Replacing it with [NSNull null]
means you have to write code when you access the array or dictionary that checks for [NSNull null]
objects.