通过NSDictionary迭代null值 [英] Iterating through NSDictionary for null value

查看:132
本文介绍了通过NSDictionary迭代null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有几个钥匙的NSDictionary,但是他们在租用部门的部门很多,例如:

I have a certain NSDictionary with couple of keys but they go much in dept in hirerchy such as:


  1. 人:

    • 性别:

      • 姓名:

        • 地址:

所以你可以看看我是否在nsdictionary中插入它,最初我只有两个键作为Person和Location,但是我试图迭代检查空值的每一个键,并将其设置为@空字符串。

So you can see if I insert this in nsdictionary, initially I just have two keys as "Person" and "Location", however I am trying to iterate in each one of the keys to check for null value and set it to @"" empty string.

有谁知道如何迭代这样的部门?

Does anyone know how to iterate through such dept?

谢谢

推荐答案


  • 你不能在 NSDictionary 中存储 nil 。要么你要找 [NSNull null] ,否则你会找到缺少你想要的键的字典....

    • you can't store a nil in an NSDictionary. Either you'll be looking for [NSNull null] or you'll be looking for dictionaries that lack the keys you are looking for....

      enumerateKeysAndObjectsUsingBlock:比($ in ...) $ c>。

      enumerateKeysAndObjectsUsingBlock: is faster than for( ... in ...).

      如果要修改字典的内容,则必须使用可变字典。如果您从属性列表中取消归档,则可以选择使用不可变节点创建可变容器(这可能是您想要的)。

      if you are modifying the contents of the dictionaries, you must use mutable dictionaries. If you are unarchiving from a property list, there are options for creating mutable containers with immutable nodes (which is probably what you want).

      Recurse是答案,但不显示递归的答案是正确的。

      Recurse is the answer, though non of the answers that show recursion are correct.

      - (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 ...
                  }
            }];
      }
      

      这篇关于通过NSDictionary迭代null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆