TouchJSON, dealing with NSNull

For another JSON library, but with the same issues, I’ve created the following category on NSDictionary:

@implementation NSDictionary (Utility)

// in case of [NSNull null] values a nil is returned ...
- (id)objectForKeyNotNull:(id)key {
   id object = [self objectForKey:key];
   if (object == [NSNull null])
      return nil;

   return object;
}

@end

Whenever I deal with JSON data from said library, I retrieve values like this:

NSString *someString = [jsonDictionary objectForKeyNotNull:@"SomeString"];

This way the code in my projects become a lot cleaner and at the same time I don’t have to think about dealing with [NSNull null] values and the like.

Leave a Comment