この質問はこの質問に似ていますが、この方法は辞書のルートレベルでのみ機能します。
出現する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
ネストされた辞書を含むすべての辞書からすべての値を見つける方法はありますか...?
編集: データはJSONフィードから取得されているため、受け取るデータは動的です(フィードが変更されるたびにアプリを更新する必要はありません)。