我有一个plist文件,其中包含以下内容:
<plist>
<dict>
<key>Rows</key>
<array>
<dict>
<key>Asia</key>
<string>India</string>
<array>
<dict>
<key>Description</key>
<string>Country</string>
</dict>
</array>
</dict>
<dict>
<key>Europe</key>
<string>Germany</string>
<array>
<dict>
<key>Germany</key>
<string>Berlin</string>
<array>
<dict>
<key>Description</key>
<string>State</string>
</dict>
</array>
</dict>
</array>
</dict>
</array>
我将此内容存储在NSDictionary中。从这里可以看出我只能得到那些键是“描述”的对象。我找不到任何解决方案,因为关键字“描述”没有固定位置意味着在plist中可以有任意数量的子级别。但是这个键永远是最后一个子级别。
答案 0 :(得分:1)
您需要通过字典进行递归,并获取每个字典的列表,其中包含一个描述键。这样的事情(未经测试):
NSDictionary *rootLevel = //dictionary from plist
NSArray *dictionariesWithKey = [self dictionariesWithDescriptionKeyFromDictionary:rootLevel];
- (NSArray *)dictionariesWithDescriptionKeyFromDictionary:(NSDictionary *)dict
{
NSMutableArray *dictionaries = [NSMutableArray array];
[dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id val, BOOL *stop) {
if ([key isEqualToString:@"description"])
[dictionaries addObject:dict];
else if ([val isKindOfClass:[NSDictionary class]])
[dictionaries addObjectsFromArray:[self dictionariesWithDescriptionKeyFromDictionary:val]];
}];
return dictionaries;
}