1

I have a plist in the following form:

Root (array)---> item 1 (dictionary) ----> Sch (string) ---> Name (string) ----> price (number) ----> item 2 (dictionary)----> .....same as item 1

How can I access each row (item1 to ...) and the its child (Sch, Name etc.)? One at a time?

I use:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];   

to load the file. How should I go about accessing each child?

What I am trying to do is, I have a NSString *message, what I want to do is to search the whole plist for matching string and display the whole item 1. Any suggestion?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stefan
  • 303
  • 1
  • 3
  • 7

2 Answers2

2

When you initialize a collection from a plist, the type is the root level object. Therefore you would not initialize a dictionary but an array like so:

NSArray *plistData = [[NSArray arrayWithContentsOfFile:finalPath] retain];

Then you would access it like this:

NSString *sch; 
NSString *name;
NSString *price;
for (NSDictionary *aDict in plistData) {
    sch = [aDict objectAtKey:"Sch"];
    name = [aDict objectAtKey:"Name"];
    price = [aDict objectAtKey:"price"];
    //.. do whatever
}
TechZen
  • 64,370
  • 15
  • 118
  • 145
1

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. @TechZen's solution is on target, I just thought this question would add an additional resource. Hope this helps!

Community
  • 1
  • 1
Mark7777G
  • 1,246
  • 2
  • 16
  • 26