How to save a value in a plist file in iphone?

2019-02-27 12:00发布

I am new to iphone development.I am using the below code to add the value to the plist but when check the plist after executing the code i dont see any value saved in the plist.Where do i go wrong? please help me out.The plist i have created is in the resource folder.Thanks.

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
[plistDict setValue:@"hello" forKey:@"choice"];
[plistDict writeToFile:finalPath atomically: YES];

For retrieving the value

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"regvalue.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:finalPath];
NSString *value;
value = [plistDict objectForKey:@"choice"];
NSLog(@"the value is %@",value);

It gives only null value.Please help me out.Thanks.

标签: iphone plist
2条回答
一夜七次
2楼-- · 2019-02-27 12:13

The resources folder is not writeable. You should save it into the documents folder.

查看更多
相关推荐>>
3楼-- · 2019-02-27 12:15

Maybe you can find the answer here: How to write data in plist?

But if you only want save some message in you app, NSUserDefaults is the better way.

查看更多
登录 后发表回答