Replace occurrences of NSNull in nested NSDictionary

A small modification to the method can make it recursive:

@interface NSDictionary (JRAdditions)
- (NSDictionary *) dictionaryByReplacingNullsWithStrings;
@end

@implementation NSDictionary (JRAdditions)

- (NSDictionary *) dictionaryByReplacingNullsWithStrings {
    const NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary: self];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in self) {
        const id object = [self objectForKey: key];
        if (object == nul) {
            [replaced setObject: blank forKey: key];
        }
        else if ([object isKindOfClass: [NSDictionary class]]) {
            [replaced setObject: [(NSDictionary *) object dictionaryByReplacingNullsWithStrings] forKey: key];
        }
    }
    return [NSDictionary dictionaryWithDictionary: replaced];
}

Note that the fast-enumeration is now on self instead of replaced

With the code above, this example:

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
[dic1 setObject: @"string 1" forKey: @"key1.1"];
[dic1 setObject: [NSNull null] forKey: @"key1.2"];

NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
[dic2 setObject: @"string 2" forKey: @"key2.1"];
[dic2 setObject: [NSNull null] forKey: @"key2.2"];

[dic1 setObject: dic2 forKey: @"key1.3"];

NSLog(@"%@", dic1);
NSLog(@"%@", [dic1 dictionaryByReplacingNullsWithStrings]);

renders this result:

2012-09-01 08:30:16.210 Test[57731:c07] {
    "key1.1" = "string 1";
    "key1.2" = "<null>";
    "key1.3" =     {
        "key2.1" = "string 2";
        "key2.2" = "<null>";
    };
}
2012-09-01 08:30:16.212 Test[57731:c07] {
    "key1.1" = "string 1";
    "key1.2" = "";
    "key1.3" =     {
        "key2.1" = "string 2";
        "key2.2" = "";
    };

Leave a Comment