我正在使用plist来存储我的应用用户的国家/地区数据。所以我有这个<dict>
名单,其中包含几个国家。对于每个国家有几个省份。对于每个区域有区域。而且每个地区都有很多城市。如何在plist中搜索以检索特定键的值
所以我的列表如下所示:
<dict>
<key>Country</key>
<array>
<dict>
<key>ISO</key>
<string>ES</string>
<key>Value</key>
<string>Spain</string>
<key>Children</key>
<array>
<dict>
<key>ISO</key>
<string>AL</string>
<key>Value</key>
<string>Andalucia</string>
<key>Children</key>
<array>
...
</array>
</dict>
</array>
</dict>
</array>
</dict>
最详细的节点是保存的信息城市:
<dict>
<key>ISO</key>
<string>03002</string>
<key>Value</key>
<string>ALICANTE</string>
</dict>
在数据库中,然后我存储这是在这种情况下,Alicante的唯一密钥。但在我的GUI中,我显然希望显示阿利坎特而不是数字表示。
那么有没有一种简单的方法来搜索这个plist文件来找到这个ISO的匹配值?
谢谢!
编辑
目前,我创造了这个方法。它有诀窍,但我无法真正称其为高效编码。所以,请大家指教...
- (NSString *)getCityNameForKey: (NSString *)ISO {
NSString *cityName = @"";
NSArray *citySource = [[Locations getInstance] toDataSource];
for (int i = 0; i < [citySource count]; i++) {
//Country
NSDictionary *p_dictionary = [citySource objectAtIndex:i];
NSArray *provinces = [p_dictionary objectForKey:@"Children"];
if ([provinces count] > 0) {
//Province
for (int j = 0; j < [provinces count]; j++) {
NSDictionary *r_dictionary = [provinces objectAtIndex:j];
NSArray *regions = [r_dictionary objectForKey:@"Children"];
if ([regions count] > 0) {
//City
for (int k = 0; k < [regions count]; k++) {
NSDictionary *c_dictionary = [regions objectAtIndex:k];
NSArray *cities = [c_dictionary objectForKey:@"Children"];
if ([cities count] > 0) {
for (int l = 0; l < [cities count]; l++) {
NSDictionary *result_dict = [cities objectAtIndex:l];
if ([[result_dict objectForKey:@"ISO"] isEqualToString:ISO]) {
index_country = i;
index_province = j;
index_region = k;
index_city = l;
cityName = [result_dict objectForKey:@"Value"];
return cityName;
break;
}
}
}
}
}
}
}
}
return cityName;
}
有多深,做孩子的嵌套走? – 2012-04-01 15:20:42
如说有国家 - >省 - >地区 - >城市。这就是使它复杂的imo。因为我不知道具体的城市位于哪个地区,省份或国家,如果我只有ISO值... – Jules 2012-04-01 15:22:12