Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace occurrences of NSNull in nested NSDictionary

This question is similar to this question, however this method only works on the root level of the dictionary.

I'm looking to replace any occurrence of NSNull values with an empty string, so that I can save the full dictionary to a plist file (if i add it with the NSNull's the file won't write).

My dictionary, however, has nested dictionaries inside it. Like this:

"dictKeyName" = {
    innerStrKeyName = "This is a string in a dictionary";
    innerNullKeyName = "<null>";
    innerDictKeyName = {
        "innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary";
        "innerDictNullKeyName" = "<null>";
    };
};

If I use:

@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 replaced) {
        const id object = [self objectForKey:key];
        if(object == nul) {
            [replaced setObject:blank forKey:key];
        }
    }
    return [NSDictionary dictionaryWithDictionary:replaced];
}

@end

I get something like this:

"dictKeyName" = {
    innerStrKeyName = "This is a string in a dictionary";
    innerNullKeyName = ""; <-- this value has changed
    innerDictKeyName = {
        "innerDictStrKeyName" = "This is a string in a Dictionary in another Dictionary";
        "innerDictNullKeyName" = "<null>"; <-- this value hasn't changed
    };
};

Is there a way of finding every NSNull value from all dictionaries including nested dictionaries...?

EDIT: The data is being drawn from a JSON feed, so the data I receive is dynamic (and I don't want to have to update the app everytime the feed changes).

like image 344
frddsgn Avatar asked Aug 31 '12 10:08

frddsgn


2 Answers

Following method works perfectly for any number of nested arrray of dictionary:

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

    for (NSString *key in [replaced allKeys])
    {
        id object = [replaced objectForKey:key];
        if (object == nul)
        {
            [replaced setObject:blank
                         forKey:key];
        }
        else
        if ([object isKindOfClass:[NSDictionary class]])
        {
            [replaced setObject:[self replaceNullInNested:object]
                         forKey:key];
        }
        else
        if ([object isKindOfClass:[NSArray class]])
        {
            NSMutableArray *dc = [[NSMutableArray alloc] init];
            for (NSDictionary *tempDict in object)
            {
                [dc addObject:[self dictionaryByReplacingNullsWithStrings:tempDict]];
            }
            [replaced setObject:dc
                         forKey:key];
        }
    }
    return replaced;
}

- (NSMutableDictionary *)replaceNullInNested:(NSDictionary *)targetDict
{
    // make it to be NSMutableDictionary in case that it is nsdictionary
    NSMutableDictionary *m = [targetDict mutableCopy];
    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:m];
    const id nul = [NSNull null];
    const NSString *blank = @"";

    for (NSString *key in [replaced allKeys])
    {
        const id object = [replaced objectForKey:key];
        if (object == nul)
        {
            [replaced setObject:blank
                         forKey:key];
        }
        else
        if ([object isKindOfClass:[NSArray class]])
        {
//            NSLog(@"found null inside and key is %@", key);
            // make it to be able to set value by create a new one
            NSMutableArray *a = [object mutableCopy];
            for (int i = 0; i < [a count]; i++)
            {
                for (NSString *subKey in [[a objectAtIndex:i] allKeys])
                {
                    if ([[object objectAtIndex:i] valueForKey:subKey] == nul)
                    {
                        [[object objectAtIndex:i] setValue:blank
                                                    forKey:subKey];
                    }
                }
            }
            // replace the updated one with old one
            [replaced setObject:a
                         forKey:key];
        }
    }
    return replaced;
}

I used above modified method as per my required functionality:

// Call Method

NSMutableDictionary *sortedDict = [[NSMutableDictionary alloc] init];

for (NSString *key in jobList){
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

    for (NSDictionary *tempDict in [jobList objectForKey:key])
    {
        [tempArray addObject:[self dictionaryByReplacingNullsWithStrings:tempDict]];
    }
    [sortedDict setObject:tempArray forKey:key];
}
like image 169
Harshal Wani Avatar answered Oct 20 '22 00:10

Harshal Wani


In case anyone needs this for swift 1.2, here's the snippet:

class func removeNullsFromDictionary(origin:[String:AnyObject]) -> [String:AnyObject] {
    var destination:[String:AnyObject] = [:]
    for key in origin.keys {
        if origin[key] != nil && !(origin[key] is NSNull){
            if origin[key] is [String:AnyObject] {
                destination[key] = self.removeNullsFromDictionary(origin[key] as! [String:AnyObject])
            } else if origin[key] is [AnyObject] {
                let orgArray = origin[key] as! [AnyObject]
                var destArray: [AnyObject] = []
                for item in orgArray {
                    if item is [String:AnyObject] {
                        destArray.append(self.removeNullsFromDictionary(item as! [String:AnyObject]))
                    } else {
                        destArray.append(item)
                    }
                }
                destination[key] = destArray
            } else {
                destination[key] = origin[key]
            }
        } else {
            destination[key] = ""
        }
    }
    return destination
}
like image 28
Alejandro Moya Avatar answered Oct 20 '22 00:10

Alejandro Moya