Domanda

I have a plist, and in my plist is a dictionary whose values are dictionaries, like this:

<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>

Now I wanna only read the url the key "Antenne Bayern" if in it's dictionary is "shortcut = 1" I do it like this:

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

But I don't want to do that for every Item in the dictionary. I only want to return the name like "Antenne Bayern" if "shortcut = 1". Can you help me please?

È stato utile?

Soluzione

You want to filter the keys of the dictionary based on a test of the associated values. Use keysOfEntriesPassingTest: to do this. You write the test to check the value of shortcut.

Aside:

In this line:

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

the [NSDictionary dictionaryWithDictionary:senderDictionary] should be replaced simply by senderDictionary because creating a new dictionary that will be destroyed again almost immediately is pointless.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top