Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access plist data

i have a plist which goes like this:

 <dict>  
   <key>11231654</key>
  <array>
      <string>hello</string>
      <string>goodbye</string>
  </array>
  <key>78978976</key>
  <array>
        <string>ok</string>
    <string>cancel</string>
   </array>

i've load the plist using this:

    NSString *path = [[NSBundle mainBundle] pathForResource:
                    @"data" ofType:@"plist"];
    // Build the array from the plist  
    NSMutableArray *array3 = [[NSMutableArray alloc] initWithContentsOfFile:path];  

how can i access each element of my plist? i want to search for a matching key in the plist and than search with its child. how can i do this? any suggestion?

thks in advance!

like image 462
Stefan Avatar asked Aug 16 '10 14:08

Stefan


1 Answers

Arrays are indexed. If you wanted to access the first object in an NSArray, you’d do the following:

id someObject = [array objectAtIndex:0];

If you know the object type, you could do it like this:

NSString *myString = [array objectAtIndex:0];

EDIT: You need to use an NSDictionary for the data that you have—note that it starts with a <dict> tag, not an <array> tag (though there are arrays as values).

NSString *path = [[NSBundle mainBundle] pathForResource:
                @"data" ofType:@"plist"];
// Build the array from the plist  
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *value = [dict valueForKey:@"11231654"];

Now you can do whatever you want with your array.

like image 53
Jeff Kelley Avatar answered Nov 11 '22 08:11

Jeff Kelley