我正在使用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