Reading plist file. iOS Programming

我的梦境 提交于 2019-12-23 02:45:12

问题


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

回答1:


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

eg:

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



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!