Question

I have a .plist in my Xcode project like this

    <?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">
<dict>
    <key>Steve</key>
    <dict>
        <key>married</key>
        <true/>
        <key>website</key>
        <string>http://www.abc.com</string>
    </dict>
    <key>Bill</key>
    <dict>
        <key>website</key>
        <string>http://www.cab.com</string>
        <key>married</key>
        <false/>
    </dict>
</dict>
</plist>

so now I only wanna read the website if married is true. The plist above is only an example I know it makes no sense. Can you please help me ? :) Edit I now do it like this:

if ([[dict objectForKey:@"married"] boolValue]) {
        NSLog(@"(Y)");
    }

But if I do that for every dict it's much work and I think I could do it easier but I don't know how.

Was it helpful?

Solution

I didn't understand your question clearly but as per my understanding i think your looking to implement a for each loop, so here is how you can go through the entire plist at once:

NSMutableArray *arryaOfBools =  [[NSMutableArray alloc] init];//To keep the values if you wnat
NSDictionary *dictioanry =  [[NSDictionary alloc] init];// initialize your plist
// this for each will traverse through every dictionary present in the plist
for(NSDictionary * dict in dictioanry ){
 /// you can do anything here as per your requirements 
 [arryaOfBools addObject:[dict valueForKey:@"married"]];
}

Hope this answers your question..

EDIT:

Here is the solution to your code

NSDictionary *senderDictionary = [NSDictionary dictionaryWithContentsOfFile:filePathforPlsit];

for(NSString *keyValue in senderDictionary){
    [[senderDictionary valueForKey:keyValue] valueForKey:@"shortcut"];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top