views:

32

answers:

1
+1  Q: 

access plist data

Hi, 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!

+1  A: 

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.

Jeff Kelley
how can i access the index value? 11231654 in this example
Stefan
In that case, your data should be loaded into an `NSDictionary`. I’ll update my answer.
Jeff Kelley