How to check if NSDictionary key is NULL

I am trying to check if an object exists for a 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 ie ( pagination = {};

), I get the following 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 if an object, or rather a key, exists without crashing?

Updated: When the key next_max_id

exists internally pagination

, the JSON response will be like

"pagination": {
    "next_url": "",
    "next_max_id": "13872296"



source to share

9 replies

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

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


This will not happen if your JSON data contains "pagination": {}, but it will happen if your JSON data contains "pagination": null.



if( [pagination objectForKey:@"your_key"] == nil ||  
    [[pagination objectForKey:@"your_key"] isEqual:[NSNull null]] ){
    //nil dictionary




check it like this

[pagination objectForKey:@"next_max_id"] == [NSNull null]


If that doesn't work then try

[pagination objectForKey:@"next_max_id"] == (id)[NSNull null]




The actual problem is that pagination

- nil

, you cannot call any selectors over NSNull

. As you mentioned - the problem only occurs when the pagination

JSON is empty, so you get a null object.


if (pagination && ![pagination isKindOfClass:[NSNull class]]) {
    ... // Do your things


if pagination is a valid object NSDictionary

, it objectForKey:

will return for a nonexistent key nil




Check the key exists and does not:

if([pagination objectForKey:@"next_max_id"] != nil) {
   // exists
else {
   // not exists


To check the key, there is a value null

if([pagination objectForKey:@"next_max_id"] == [NSNull null]) {
   // {"next_max_id":null, ...}
else {
   // not exists or values is not null




For example, I have created this demo that you can modify according to your needs.

NSDictionary *pagination = 
[[NSDictionary alloc] initWithObjectsAndKeys:
   @"65765343",@"next_max_id",   nil];

NSArray *array = [NSArray arrayWithArray:[pagination allKeys]];

if ( [array containsObject:@"next_max_id"] ) {
    NSLog( @"%@",[pagination valueForKey:@"next_max_id"] );
} else {
    NSLog(@"Key is null");




if( [pagination objectForKey:@"next_max_id"] == nil ||  
    [[pagination objectForKey:@"next_max_id"] isEqual:[NSNull null]] ||
    [pagination objectForKey:@"next_max_id"] == [NSNull null])
    NSLog(@"Key is null");
    NSLog(@"%@",[pagination valueForKey:@"next_max_id"]);




A very elegant solution is to write a swift category for NSDictionary.

For example:

#import "NSDictionary+safe.h"

@implementation NSDictionary (safeJSON)

- (id)safeObjectForKey:(NSString *)key {

    id object = [self objectForKey:key];
    if (object == [NSNull null]) {
        return nil;
    } else {
        return object;



This was also mentioned by rckoenes here .



- [NSNull objectForKey:]: unrecognized selector sent to the instance

The error is already in your hands.

When you call [pagination objectForKey:]

it calls-[NSNull objectForKey:]

Hence, pagination


NOT NSDictionary

. NSNull has no method objectForKey:

to call. This is why you got the error:

unrecognized selector posted to the instance

You can check it out,

if ([pagination isKindOfClass:[NSDictionary class]]) {
    id value =[pagination objectForKey:@"key"];




All Articles