views:

166

answers:

1

Hi,

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 such i go about accessing each child?

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

Thanks for the help! gd day!

A: 

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