Cocoa JSON - check if array or dictionary is being used

I am using the Cocoa JSON framework ( http://code.google.com/p/json-framework/ ) to communicate with the API.

The problem is that the API returns a dictionary if there is an error, but returns an array of results if it works.

Is there a good way to determine if a JSONValue is an array or a dictionary?

Thanks.

+3
source share
3 answers

You can use isKindOfClass:to check if the object is an instance of NSDictionary or any subclass of it.

In most cases, you are better off serving the validation respondsToSelector:, but this is one case when you are really better off testing its class membership.

, , , ; API, , , .

, , , .

+6

, length.

if (jsonObj.length) {
   //doSomeWork
}
0

Try the following:

if ([YourData isKindOfClass:[NSArray class]])
{
    NSLog(@"Array format found");
}
else
{
    NSLog(@"Dictionary format found");
}
0
source

Source: https://habr.com/ru/post/1719989/


All Articles