枚举二级NSDictionaries

时间:2012-05-14 08:03:51

标签: iphone ios nested nsdictionary enumeration

假设我有一个带有嵌套NSDictionary的NSDictionary,这样(简化,我有大约10个嵌套字典):

key1={
  nesteddictionary={
    nestkey1 = "nested value";
    nestedkey2 = "another value";
  }
  nesteddictionary2={
    nestedkey3 = "want this too";
  }
}
key2="awesome"

调用[dictionary allKeys]只会给我'key1'和'key2'。有没有什么简单的方法可以循环遍历嵌套字典中的所有键?甚至是嵌套的?

1 个答案:

答案 0 :(得分:1)

递归函数

- (void) findAllKey:(NSDictionary*)dic
{
    for ( NSString *key in [dic allKeys] )
    {
        NSLog(@"%@",key);

        if ( [[dic objectForKey:key] isKindOfClass:[NSDictionary class]] )
        {
            [self findAllKey:[dic objectForKey:key]];
        }
    }
}

深度优先搜索