gpt4 book ai didi

iphone - 遍历 NSDictionary 获取 null 值

转载 作者:行者123 更新时间:2023-12-03 16:49:03 24 4
gpt4 key购买 nike

我有一个带有几个键的 NSDictionary,但它们在招聘方面有很多作用,例如:

  1. 人物:
    • 性别:
      • 姓名:
        • 地址:
  2. 地点:

所以你可以看看我是否将其插入到 nsdictionary 中,最初我只有两个键“Person”和“Location”,但是我尝试迭代每个键以检查空值并将其设置为@""空字符串。

有人知道如何遍历这样的部门吗?

谢谢

最佳答案

  • 您无法在 NSDictionary 中存储 nil。您要么寻找[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 ...
}
}];
}

关于iphone - 遍历 NSDictionary 获取 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8477789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com