I am working with objective c for an iphone app.
I see that [dictionary objectForKey:@"key"] return <null>. Doing a if([dictionary objectForKey:@"key"] == nil || [dictionary objectForKey:@"key"] == null) does not seem to catch this case.
Doing a if([[dictionary objectForKey:@"key"] isEqualToString:@"<null>"]) causes my program to crash.
What is the correct expression to catch <null>?
More Details
An if statement for nil still isn’t catching the case… Maybe i’m just too tired to see something, but here’s additional info:
Dictionary is populated via a url that contains json data like so:
NSURL *url = [NSURL URLWithString:"http://site.com/"];
dataresult = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:dataresult options:kNilOptions error:& error];
doing an NSLog on the dictionary gives this output:
{
key = "<null>";
responseMessage = "The email / registration code combination is incorrect";
}
You have an instance of
NSNull. Actually, the instance, since it’s a singleton.Cocoa collections can’t contain
nil, although they may returnnilif you try to access something which isn’t present.However, sometimes it’s valuable for a program to store a thing meaning “nothing” in a collection. That’s what
NSNullis for.As it happens, JSON can represent null objects. So, when converting JSON to a Cocoa collection, those null objects get translated into the
NSNullobject.When Cocoa formats a string with a “%@” specifier, a
nilvalue will get formatted as “(null)” with parentheses. AnNSNullvalue will get formatted as “<null>” with angle brackets.