从NSDictionary获得所有价值(深刻)

时间:2009-10-09 14:35:13

标签: iphone iphone-sdk-3.0

我有这样的字典:

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

如何迭代所有键并获取数组中的所有值?

像这样:

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

由于

亨利

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

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