Getting all value from NSDictionary (deeply)

你离开我真会死。 提交于 2019-12-13 03:53:09

问题


I have a dictionary like this :

<dict>
  <key>ThemeName</key>
  <string>Theme1</string>
  <key>AddToFavoritesButton</key>
  <dict>
    <key>DownImage</key>
    <string>heart_selected.png</string>
    <key>UpImage</key>
    <string>heart.png</string>
 </dict>
<dict>

How to iterate all keys and getting all values in an array ?

like this :

res[0] = Theme1
res[1] = heart_selected.png
res[2] = heart.png
...

Thanks

Thierry


回答1:


Something like this should work:

- (void)expandDictionary:(NSDictionary *)dict into:(NSMutableArray *)output
{
    for(id key in [dict allKeys])
    {
        id value = [dict valueForKey:key];
        if([value isKindOfClass:[NSDictionary class]])
        {
            [self expandDictionary:value into:output];
        }
        else
        {
            [output addObject:[value stringValue]];
        }
    }
}

- (NSArray *)expandDictionary:(NSDictionary *)dictionary
{
    NSMutableArray *output = [NSMutableArray array];

    [self expandDictionary:dictionary into:output];

    return output;
}


来源:https://stackoverflow.com/questions/1544167/getting-all-value-from-nsdictionary-deeply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!