2016-12-22 95 views
0

我想從應該排序的項目的plist文件中獲取數據。現在我的plist看起來像在NSDictionary中從plist獲取數據

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
     <key>code</key> 
     <string>AF</string> 
     <key>phone_code</key> 
     <string>93</string> 
     <key>name</key> 
     <string>Afghanistan</string> 
    </dict> 
    <dict> 
     <key>code</key> 
     <string>AL</string> 
     <key>phone_code</key> 
     <string>355</string> 
     <key>name</key> 
     <string>Albania</string> 
    </dict> 

等高達上最後country.And我獲取數據這樣

NSString* path = [[NSBundle mainBundle] pathForResource:@"CountriesWithPhoneCodes" ofType:@"plist"]; 
    NSArray* a = [NSArray arrayWithContentsOfFile:path]; 
    for (NSDictionary *d in a) 
    { 
     NSString *cdate = [d objectForKey:@"CDate"]; 
     if ([cdate isEqualToString:@"Whatever is currently selected"]) 
     { 
      NSString *text = [d objectForKey:@"Text"]; 
      // do something with text here 
     } 
    } 

    NSDictionary *dict = [[NSDictionary alloc]init]; 
    dict = [self indexKeyedDictionaryFromArray:a]; 

    NSLog(@"This is %@",[[dict allValues]objectAtIndex:0]); 

我得到的輸出是

This is { 
    code = AF; 
    name = Afghanistan; 
    "phone_code" = 93; 
} 

從中我我無法正確訪問數據。如何創建一個phone_code作爲值和國家/地區名稱作爲關鍵字的字典。

+0

你想訪問什麼?這是{code = AF; name = Afghanistan; 「phone_code」= 93; } –

+0

這很不清楚。 'd'沒有鑰匙''CDate'''。您可以使用謂詞或'indexOfObjectPassingTest:'來查找您想要的字典。另外,如果我們認爲國家/地區名稱是唯一的,則可以使用國家/地區名稱作爲關鍵字並使用頂級字典,而不是使用數組。 – Larme

+0

你是什麼意思'我無法正確訪問數據'?你寧願只需要一個只有'name'和'phone_code'的字典? – dirtydanee

回答

1

NSPredicate完全符合您的需求。

NSString *path = [[NSBundle mainBundle] pathForResource:@"CountriesWithPhoneCodes" ofType:@"plist"]; 
NSArray *array = [NSArray arrayWithContentsOfFile:path]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"phone_code = %@", @"String variable containing code"]; 
NSDictionary *countryDictionary = [array filteredArrayWithPredicate:predicate].firstObject; 

如果你想創建字典,這樣的結構:{"phone_code":"country_code"}

NSMutableDictionary *phonesDict = [NSMutableDictionary new]; 
for (NSDictionary *dict in array) { 
    NSString *phoneCode = dict[@"phone_code"]; 
    NSString *countryCode = dict[@"country_code"]; 
    phonesDict[countryCode] = phoneCode; 
} 

注意:您要保存這本詞典的地方,以避免訪問磁盤上的文件。

0
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]]; 

// Your dictionary contains an array of dictionary 
// Now pull an Array out of it. 
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]]; 

// Now a loop through Array to fetch single Item from catList which is Dictionary 
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) { 
// Fetch Single Item 
// Here obj will return a dictionary 
NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]); 
NSLog(@"Category id : %@",[obj valueForKey:@"cid"]); 
}];