开发者

access plist data

开发者 https://www.devze.com 2023-01-11 14:09 出处:网络
i have a plist which goes like this: <dict> <key>11231654</key> <array> <string>hello</string>

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>开发者_JAVA技巧;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!


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.


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!


This is how you need to create plist

access plist data

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.!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号