sam
sam

Reputation: 117

Reading plist file. iOS Programming

I have this code and can't figure out what I'm doing wrong. As you can see in the code below I have a plist file called shifts.plist which is in my supporting files folder. Here is my plist structure.
enter image description here

    NSString *path = [[NSBundle mainBundle] pathForResource:@"shifts" ofType:@"plist"];
    dictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    cell.textLabel.text = [secondTableInfo objectAtIndex:indexPath.row];
    NSLog(@"%@",[[dictionary objectForKey:@"name"]objectAtIndex:0]);

I would ultimately like to read the name entries and populate a UITableView with them.
I used NSLog to output dictionary and I got the following. So the file is there it's just the parsing that I'm getting wrong.
NSLog output
Thanks,
Sam

Upvotes: 1

Views: 7690

Answers (2)

Anil Kothari
Anil Kothari

Reputation: 7733

Main Error:- root is array and you are taking file into dictionary.So declare a NSArray in .h file and retain,nonatomic its property.

NSString *path = [[NSBundle mainBundle] pathForResource:@"shifts" ofType:@"plist"];

array = [[NSArray alloc]initWithContentsOfFile:path];

NSLog(@"First Index Name %@",[[array objectAtIndex:0] objectForKey:@"Name"]);

I am sure it will answer your question.

Upvotes: 1

zeroimpl
zeroimpl

Reputation: 2846

Looks like you need to call objectAtIndex: first, then call objectForKey:

eg:

[[dictionary objectAtIndex:0] objectForKey:@"Name"]

Upvotes: 1

Related Questions