.plist如何在dict中读取dict

时间:2014-03-29 13:34:22

标签: ios objective-c macos filter nsdictionary

我有一个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>

现在我只想阅读关键字&#34; Antenne Bayern&#34;如果在它的字典中是&#34; shortcut = 1&#34; 我是这样做的:

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"];

但我不想为字典中的每个项目做这件事。我只想回复像#34; Antenne Bayern&#34; if&#34; shortcut = 1&#34;。 你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您希望根据相关值的测试过滤字典的键。使用keysOfEntriesPassingTest:执行此操作。您编写测试以检查shortcut

的值

除了:

在这一行:

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

[NSDictionary dictionaryWithDictionary:senderDictionary]应该只由senderDictionary替换,因为创建一个几乎立即被再次销毁的新词典是毫无意义的。