替换嵌套 NSDictionary 中出现的 NSNull [英] Replace occurrences of NSNull in nested NSDictionary

查看:25
本文介绍了替换嵌套 NSDictionary 中出现的 NSNull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于这个问题,但是这个方法仅适用于字典的根级别.

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

我希望用空字符串替换任何出现的 NSNull 值,以便我可以将完整字典保存到 plist 文件(如果我将它与 NSNull 的文件一起添加,文件将不写).

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>";
    };
};

如果我使用:

@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

我得到了这样的东西:

"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
    };
};

有没有办法从包括嵌套字典在内的所有字典中找到每个 NSNull 值?

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

数据是从 JSON 提要中提取的,因此我收到的数据是动态的(我不想每次提要更改时都必须更新应用).

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

推荐答案

对方法稍加修改即可使其递归:

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];
}

请注意,快速枚举现在位于 self 而不是 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]);

呈现这个结果:

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" = "";
    };

这篇关于替换嵌套 NSDictionary 中出现的 NSNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆