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作爲值和國家/地區名稱作爲關鍵字的字典。
你想訪問什麼?這是{code = AF; name = Afghanistan; 「phone_code」= 93; } –
這很不清楚。 'd'沒有鑰匙''CDate'''。您可以使用謂詞或'indexOfObjectPassingTest:'來查找您想要的字典。另外,如果我們認爲國家/地區名稱是唯一的,則可以使用國家/地區名稱作爲關鍵字並使用頂級字典,而不是使用數組。 – Larme
你是什麼意思'我無法正確訪問數據'?你寧願只需要一個只有'name'和'phone_code'的字典? – dirtydanee