How can we identify the object is available for a particular key. I have tried following:
for(NSDictionary *item in jsonArray){
if([item objectForKey:@"EventDate"])
NSLog([item objectForKey:@"EventDate"]);
}
This is getting crash the code with error:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x6a567b0
I have also find many posts that is showing objectForKey will return nil if a key doesn’t exists. Than my question is there is also a method in NSDictionary class that is “setNilValueForKey”. How is this possible that we cannot specify the NSDictionary key with nil object and also we have the method to set nil value for object in NSDictionary.
Please Suggest on first and also make me clear on second query.
1) Your jsonArray contains other types of objects than
NSDictionaries, including at least oneNSString.NSStringdoesn’t respond to objectForKey: so it throws an exception when you try to call it. You’ll have to look at the JSON to determine how to proceed with whatever you were doing.2) There is an
NSObjectmethodsetNilValueForKey:which is related to key-value coding. This isn’t really related toNSDictionary. If you really need to represent nil in your dictionary, set[NSNull null]as the object for your key that represents nil.Hope this helps!