How to remove a null value from NSDictionary

jimbob picture jimbob · Jan 3, 2013 · Viewed 14.7k times · Source

I have a JSON Feed:

{
    "count1" = 2;
    "count2" = 2;
    idval = 40;
    level = "<null>";
    "logo_url" = "/assets/logos/default_logo_medium.png";
    name = "Golf Club";
    "role_in_club" = Admin;
}

The problem is the "<null>". I cannot figure out how to remove it from the NSDictionary before saving it to NSUserDefaults.

Answer

Martin R picture Martin R · Jan 3, 2013

Another variation, without (explicit) loop:

NSMutableDictionary *dict = [yourDictionary mutableCopy];
NSArray *keysForNullValues = [dict allKeysForObject:[NSNull null]];
[dict removeObjectsForKeys:keysForNullValues];