Getting all value from NSDictionary (deeply)
I have a dictionary like this :
<dict>
<key>ThemeName</key>
<string>Theme1</string>
<key>AddToFavoritesButton</key>
<dict>
<key>DownImage</key>
<string>h开发者_如何转开发eart_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
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;
}
精彩评论