Replace occurrences of NSNull in nested NSDictiona

2019-01-09 13:04发布

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).

8条回答
beautiful°
2楼-- · 2019-01-09 13:40

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
}
查看更多
相关推荐>>
3楼-- · 2019-01-09 13:41

This solution works with arrays and dictionaries, and also for nested arrays and dictionaries etc (recursively).

- (NSDictionary *)writableDictionary:(NSDictionary *)dictionary
{
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];

    for (id key in mutableDictionary.allKeys)
    {
        id value = mutableDictionary[key];
        mutableDictionary[key] = [self writableValue:value];
    }

    return mutableDictionary;
}

- (NSArray *)writableArray:(NSArray *)array
{
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];

    for (int i = 0; i < mutableArray.count; ++i)
    {
        id value = mutableArray[i];
        mutableArray[i] = [self writableValue:value];
    }

    return mutableArray;
}

- (id)writableValue:(id)value
{
    if ([value isKindOfClass:[NSNull class]])
    {
        value = @"";
    }
    else if ([value isKindOfClass:[NSDictionary class]])
    {
        value = [self writableDictionary:value];
    }
    else if ([value isKindOfClass:[NSArray class]])
    {
        value = [self writableArray:value];
    }
    return value;
}
查看更多
登录 后发表回答