cocoacoder
cocoacoder

Reputation: 391

reading a plist file in Cocoa

I have a plist that looks as below. Whats the preferred method to read a plist into and how do I read this into an array ? I can create a NSMutableDictionary, load the contents of this plist, load the keys into an Array and then use a for loop to read the top 2 keys - SomeData and MoreData. But I am unable to parse all the other keys (Key1, Key2 and Key3). How would I go about doing this ? Pasted code below that goes through the top 2 keys. (SomeData and MoreData). Thanks.

Plist:

<?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>SomeData</key>
    <array>
        <dict>
             <key>Key1</key>
             <false/>
             <key>Key2</key>
             <dict>
                       <key>Key3</key>
                       <integer>0</integer>
                  </dict>
            </dict>
       </array>
       <key> MoreData</key>
   </dict>  
  </plist>       

Code:

-(IBAction)modifyPlist:(id)sender{

    NSLog(@"Listing Keys");
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/Users/sureshb/Documents/myprogs/plistMod/com.test.plist"];

    NSArray *keys = [dict allKeys];

    for (NSString *key in keys){
        NSLog(@"%@",key);
        }

}

Upvotes: 3

Views: 3117

Answers (1)

Nekto
Nekto

Reputation: 17877

If I'm not mistaken you can access those values in next way:

NSArray *someData = [dict objectForKey:@"SomeData"];
BOOL key1 = [[[someData objectAtIndex:0] valueForKey:@"Key1"] boolValue];
int key2 = [[[[someData objectAtIndex:0] objectForKey:@"Key2"] objectForKey:@"Key3"] intValue];

Upvotes: 3

Related Questions