2012-04-03 76 views
1

我有一個plist中,看起來像這樣:如何從字典中讀出數據?

<dict> 
<key>Aaron</key> 
<dict> 
    <key>number</key> 
    <string>1234</string> 
    <key>country</key> 
    <string>Spanien</string> 
    <key>year</key> 
    <string>1989</string> 
</dict> 
<key>Abagail</key> 
<dict> 
    <key>number</key> 
    <string>1234</string> 
    <key>country</key> 
    <string>Frankrike</string> 
    <key>year</key> 
    <string>2001</string> 
</dict> 
<key>Abbey</key> 
<dict> 
    <key>number</key> 
    <string>1289</string> 
    <key>country</key> 
    <string>Tyskland</string> 
    <key>year</key> 
    <string>2009</string> 
</dict> 

有了這個代碼,我得到每個對象的關鍵:

NSLog(@"test %@", [dict objectForKey:key2]); 

我怎樣才能得到每個字符串值目的。我viewDidLoad中看起來像這樣

NSString *path = [[NSBundle mainBundle] pathForResource:@"vin" 
               ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path]; 

self.allNames = dict; 

NSString *key2 = viewTitle; 

NSLog(@"test %@", [dict objectForKey:key2]); 
+0

這個問題不清楚,你是否真的成功讀取文件? – borrrden 2012-04-03 13:51:02

+0

爆炸地鐵隧道讓我雙重評論。 – borrrden 2012-04-03 13:52:03

+0

是的,我可以讀出文件。 – user1301000 2012-04-03 14:30:48

回答

2

基於以上的plist,你可以得到例如訪問到country的值:

NSDictionary* record2 = (NSDictionary*)[dict objectForKey:key2]; 
NSString* country = (NSString*)[record2 objectForKey:@"country"]; 
+0

謝謝!!!幫助很大。你是最棒的! – user1301000 2012-04-03 14:37:26

+0

很高興聽到它解決了你的問題 – MrTJ 2012-04-03 14:59:08

+0

這是什麼'key2'?索引? – GoodSp33d 2013-03-18 05:33:25