How to store value in plist file in iphone?

I am new to iphone development. I am using the below code to add a value to the plist, but when checking the plist after executing the code, I cannot see any value stored in the plist. Where am I going wrong? please help me. The plastic I created is in the resources 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];

      

To get 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 only gives a null value. Please help me. Thanks.

+2


source to share


2 answers


The resource folder could not be written. You must save it in your documents folder.



+5


source


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



But if you want to keep some kind of message in your application, your best bet is to use NSUserDefaults.

0


source







All Articles