iOS checking if JSON return has Key
-
18-06-2021 - |
Question
I'm using NSJsonSerialization to get back a json return after I POST something. I either get back my correct return or I get:
{
"status":"false"
}
I need to check if I got back my return before I try to parse it (since it crashes if the Keys I ask for don't exist) However if I check against
[whatever objectForKey:@"status"] == nil
When it is nil it's ok, however if it does not exist it crashes (because I got back my other return)
The crash I get is this one, it seems NSJSONSerialzation is returning a NSArray as a NSdictionary?:
2012-07-02 11:03:40.426 [9412:10703] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x75f03f0
2012-07-02 11:03:40.429 [9412:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x75f03f0'
I need to figure out a way to know which return I got so I can act accordingly.
Solution
you could check the type of whatever
variable
if([whatever isKindOfClass:[NSDictionary class]])
//then check for nil
else
//Then its not a dictionary then in your case its empty
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow