这个问题类似于这个问题 https://stackoverflow.com/questions/8075147/replace-all-nsnull-objects-in-an-nsdictionary,但是此方法仅适用于字典的根级别。
我正在寻找替换任何出现的NSNull
值带有空字符串,这样我就可以将完整的字典保存到 plist 文件中(如果我将其与 NSNull 添加,则文件将不会写入)。
然而,我的字典里面有嵌套字典。像这样:
"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
所有字典(包括嵌套字典)的值...?
EDIT:数据是从 JSON feed 中提取的,因此我收到的数据是动态的(并且我不想每次 feed 更改时都更新应用程序)。
对该方法进行小的修改可以使其递归:
@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
有了上面的代码,这个例子:
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" = "";
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)