【问题标题】:Getting all value from NSDictionary (deeply)从 NSDictionary 获取所有价值(深度)
【发布时间】:2009-10-09 14:35:12
【问题描述】:
我有一本这样的字典:
<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
...
谢谢
蒂埃里
【问题讨论】:
标签:
iphone
iphone-sdk-3.0
【解决方案1】:
这样的事情应该可以工作:
- (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;
}