Tag Archives: NSDictionary

How to safely work with NSDictionary that may have empty values or may not have a key

Dictionary_2x

NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may have empty values or may not have a key.

The method objectForKey will return nil, if there is no a key, a key doesn’t exist in a dictionary.

You can’t add nil to a NSDictionary. NSDictionary will throw an NSInvalidArgumentException if you attempt to insert a nil value. You would have to use [NSNull null] instead. NSNull is something called nothing, while nil is simply nothing.

Checks that value for key is not empty.

But if a dic doesn’t contain a key, you will enter to the body of a condition dic[@”key”] != (id)[NSNull null].

For example, before taking integerValue from obj, you must be sure, that it is not [NSNull null]:

Checks that the key exists, but value for it can still be empty.

So the optimal check while reading NSDictionary is this, where key is “key”

You can’t add nil to a NSDictionary. So you have to check, that your object is not nil before adding:

But you can add [NSNull null]:

Version of code, where you check if dictionary entry exists and not empty:

I have created a NSDictionary category that has a method to check if a key exists and value is not empty:

This is an example, how to use it: