在嵌套的NSDictionary中替换NSNull的出现次数 [英] 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.
我希望用 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 Feed,因此我收到的数据是动态的(我不想每次更改Feed时都要更新应用程序)。
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];
}
请注意,快速枚举现在位于 自我
而不是 已取代
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屋!