2014-03-29 139 views
0

我有一個plist中,在我的plist是一本字典,其值是字典,像這樣:的.plist如何讀字典在字典

<dict> 
    <key>Antenne Bayern</key> 
    <dict> 
     <key>shortcut</key> 
     <true/> 
     <key>url</key> 
     <string>http://www.antenne.de/webradio/antenne-aac.pls</string> 
    </dict> 
    <key>Technobase.fm</key> 
    <dict> 
     <key>url</key> 
     <string>http://listen.technobase.fm/tunein-aacplus-pls</string> 
     <key>shortcut</key> 
     <false/> 
    </dict> 
</dict> 

現在我想只讀URL中的關鍵「ANTENNE拜仁「如果在它的字典是‘快捷= 1’ 我不喜歡這樣寫道:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"SenderDatabase" ofType:@"plist"]; 
NSDictionary *senderDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 
NSDictionary *bli = [[NSDictionary dictionaryWithDictionary:senderDictionary] valueForKey:@"Antenne Bayern"]; 
NSLog(@"%@", bli); 
NSString *check = [bli valueForKeyPath:@"shortcut"]; 

但我不想這樣做,在字典中的每個項目。如果「快捷方式= 1」,我只想返回「Antenne Bayern」這個名字。 你能幫我嗎?

回答

0

您想基於關聯值的測試過濾字典的鍵。使用keysOfEntriesPassingTest:來做到這一點。你寫測試來檢查shortcut的值。

旁白:

在這一行:

[[NSDictionary dictionaryWithDictionary:senderDictionary] valueForKey:@"Antenne Bayern"]; 

[NSDictionary dictionaryWithDictionary:senderDictionary]應該簡單地用senderDictionary替換,因爲創建將再次幾乎立即被摧毀一個新的字典是沒有意義的。