How to check if NSDictionary key is NULL

Scott

I'm trying to check if an object exist for the key next_max_id in my JSON response dictionary. I've tried the following ways to check:

  • if ([[pagination objectForKey:@"next_max_id"] isKindOfClass:[NSNull class]]) ...
  • if ([pagination objectForKey:@"next_max_id"] == [NSNull class]) ...
  • if ([pagination objectForKey:@"next_max_id"]) ...

However, when the pagination dictionary is empty i.e. (pagination = {};), I get these error messages:

-[NSNull objectForKey:]: unrecognized selector sent to instance ...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance ...

How else can I check for if the object or rather the key exist, without a crash?

Updated: When the key next_max_id exist inside pagination, the JSON response would be like this

"pagination": {
    "next_url": "https://api.instagram.com/v1/tags/puppy/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d&max_id=13872296",
    "next_max_id": "13872296"
}
gnasher729

With the error that you describe, pagination is not an NSDictionary, but pagination itself is an NSNull object. So the first check before everything else would be

if (pagination == [NSNull null]) ...

That wouldn't happen if your JSON data contains "pagination": {} but it would happen if your JSON data contains "pagination": null .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a nsdictionary key has values or not?

How to check if an NSDictionary or NSMutableDictionary contains a key?

Check key exists in NSDictionary

How to check NSDictionary for Key Value pairs without making it crash?

How to check if dictionary key is null

How to get key of NSDictionary

Check NSArray contain NSDictionary key in Swift?

How to access NSDictionary by key number?

How to check SharedPreferences key if not empty or null?

how to make a new NSDictionary from a NSDictionary where key = x

JSON to NSDictionary - How to check the boolean type

How to check if NSDictionary is not nil in Swift 2

How to check prefix of string inside an NSDictionary?

How to filter a NSArray by Key:value, that is in a NSDictionary

how to get key of selected value from nsdictionary

How to get key and value from an NSDictionary?

How to get the value for key @int from NSDictionary?

NSDictionary - Need to check whether dictionary contains key-value pair or not

How to remove a string containing "<null>" from an NSDictionary

php, key check on NULL variable gets set, how to?

In Cloud Firestore rules - How do I check if a key is null

"Postman -How to check the typeof any key is 'string' or 'null' at same time"

Joi - how to check when other key's value is null/empty

How to create a NSDictionary with blank key/ without key for an object

Check if dictionary value of key is null

How to check if objects in an array is null. i.e. the key value pair is null

How to access indices of an NSDictionary, and convert that key’s value to an Int in Swift?

How to get the a value of particular key out of Cocoa's NSDictionary?

How to add one more object and key to an existing NSDictionary?