0

假设我有一个像这样嵌套 NSDictionary 的 NSDictionary(简单来说,我有大约 10 个嵌套字典):

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

打电话[dictionary allKeys]只给我'key1'和'key2'。有没有什么简单的方法可以像这样遍历嵌套字典中的所有键?即使是嵌套的?

4

1 回答 1

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

这是深度优先搜索

于 2012-05-14T08:08:30.550 回答