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.
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.
Thanks,
Sam
Looks like you need to call objectAtIndex:
first, then call objectForKey:
eg:
[[dictionary objectAtIndex:0] objectForKey:@"Name"]
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.
来源:https://stackoverflow.com/questions/8146023/reading-plist-file-ios-programming