从NSDictionary获得所有价值(深入) [英] Getting all value from NSDictionary (deeply)

查看:47
本文介绍了从NSDictionary获得所有价值(深入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字典:

<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 ?

像这样:

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

谢谢

蒂埃里

推荐答案

类似的方法应该起作用:

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

这篇关于从NSDictionary获得所有价值(深入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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