2012-04-01 58 views
1

我正在使用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; 
} 
+0

有多深,做孩子的嵌套走? – 2012-04-01 15:20:42

+0

如說有國家 - >省 - >地區 - >城市。這就是使它複雜的imo。因爲我不知道具體的城市位於哪個地區,省份或國家,如果我只有ISO值... – Jules 2012-04-01 15:22:12

回答

3

您可以在plist中加載到NSDictionary與方法:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 

然後,只需訪問該值,你會爲任何字典:

NSDictionary *region = [dict valueForKey:@"City"]; 
NSString *name = [region objectForKey:@"Value"]; 
+0

但是,由於我有4個級別的孩子,所以這不起作用。不只是一個.. – Jules 2012-04-01 16:00:07

1

我沒有足夠的數據來真實地描繪它,但是您可以使用keyPath獲取所有城市的數組,然後在此數組上使用謂詞

像這樣的事情將是你如何開始所有的數據放在一個陣列開始

NSArray *cities = [countries valueForKeyPath:@"Children.Children"]; 

然後進行搜索,你可以做這樣的事情

__block NSString *name = nil; 

[cities enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    if ([[obj valueForKey:@"ISO"] isEqualToString:ISOValue]) { 
     name = [obj valueForKey:@"Value"]; 
     *stop = YES: 
    } 
}];