access plist data
Asked Answered
S

3

6

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!

Sage answered 16/8, 2010 at 14:23 Comment(0)
S
9

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.

Salpingectomy answered 16/8, 2010 at 14:26 Comment(2)
how can i access the index value? 11231654 in this exampleSage
In that case, your data should be loaded into an NSDictionary. I’ll update my answer.Salpingectomy
R
2

Here is a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. @Jeff Kelley's solution is on target, I just thought this question would add an additional resource. Hope this helps!

Roxannaroxanne answered 6/2, 2011 at 16:32 Comment(0)
J
1

This is how you need to create plist

plist screenshot

Set root to array instead of dictionary , this will give you results in sequence. With below lines of code you can fetch complete plist.

NSString *pathOfPlist = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
arrPlistValue = [[NSArray alloc]initWithContentsOfFile:path];
NSLog(@"Plist Value : %@",arrPlistValue);

After that you can fetch any specific content with below code:

NSString *strName = [[arrTempValue objectAtIndex:0] objectForKey:@"name"];
NSString *strImage = [[arrTempValue objectAtIndex:0] objectForKey:@"image"];
int idValue = [[[arrTempValue objectAtIndex:0] objectForKey:@"id"] integerValue];
BOOL isSubCat = [[[arrTempValue objectAtIndex:0] objectForKey:@"isSubCategory"] boolValue];

With this code you can fetch integer , string , boolean from plist.!

Jenevajeni answered 14/8, 2013 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.