我有一个带有几个键的 NSDictionary,但它们在招聘方面有很多作用,例如:
- Person:
- 地点:
所以你可以看看我是否将其插入 nsdictionary 中,最初我只有两个键“Person”和“Location”,但是我试图迭代每个键以检查空值并将其设置为 @""空字符串。
有谁知道如何迭代这样的部门?
Thanks
你不能存储一个nil
in an NSDictionary
。要么你会寻找[NSNull null]
或者你会寻找缺少你正在寻找的键的字典......
enumerateKeysAndObjectsUsingBlock:
比for( ... in ...)
.
如果要修改字典的内容,则必须使用可变字典。如果您从属性列表中取消归档,可以选择使用不可变节点创建可变容器(这可能就是您想要的)。
递归是答案,尽管显示递归的答案都不正确。
- (void)filterMutableDictionary:(NSMutableDictionary*)aDictionary
{
// check if aDictionary is missing any keys here
if (![aDictionary objectForKey:@"DesiredKey"]) {
... fill in key/value here ...
}
// enumerate key/values, filtering appropriately, recursing as necessary
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass: [NSMutableDictionary class]]) {
[self filterMutableDictionary: value];
} else {
... filter value here ...
}
}];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)