Warrior
Warrior

Reputation: 39374

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

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 don't see any value saved in the plist. Where did I go wrong? The plist I have created is in the resource folder.

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.

Upvotes: 2

Views: 7645

Answers (2)

kimimaro
kimimaro

Reputation: 1263

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.

Upvotes: 0

Jasarien
Jasarien

Reputation: 58448

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

Upvotes: 5

Related Questions