I was recently told that casting [NSNull null] to (NSString*) was "terrible."
However, the method -[NSDictionary stringForKey:] returns [NSNull null] if the key is not in the dictionary, and if I do not perform the cast, the compiler yells at me.
Am I missing something?
EDIT:
My mistake ... and I think I'm starting to see the problem ...
As everyone noted, there is no stringForKey: method in NSDictionary, and yes, I was thinking about custom defaults when I asked a question ... so I came back and looked what I was doing ... here it is:
NSString * someValue = (NSString*)[myDictionary objectForKey:@"key"]; if (someValue == (NSString*)[NSNull null]) ...
If I do not perform the cast in this case, I get the following error:
warning: comparison of distinct Objective-C types 'struct NSNull *' and 'struct NSString *' lacks a cast
Casting the value "solves" the problem, and I would prefer not to write 10 lines, where one will do ...
This is bad? Where did I encounter problems?
ALSO:
Dictionaries come from the JSON library ... and NULL are valid values in this case, so it’s possible that it’s not that NSDictionary returns them if the key is missing, but rather that the key is essentially there, and the value is actually equals zero.
objective-c iphone
Steve
source share