tags:

views:

463

answers:

1

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.

+1  A: 

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

Jasarien
I don't have any folder names documents, u want me to have it with class older
Warrior
Your app will have a documents folder after being installed to the device or simulator. All apps have a basic set of directories in their sandbox, (this isn't Mac OS X where your resource folder is writeable). Each App sandbox has the app bundle, the documents folder, a temp folder and a cache folder.
Jasarien
Still i don't get u.I am a newbie.I don't know about sandbox.I am not able to find document folder yet.please let me know inside which folder of my xcode project should i put the plist file.Thanks.
Warrior
If you're writing the plist using code, then you don't need to put it anywhere in your Xcode project. After your app is installed, the folders I mentioned are created. You should use `NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];` to get the path to the documents directory and then append your filename to the path and write it to the file using the `writeToFile:atomically` method. This is all explained in the documentation which you should read.
Jasarien
i have created the plist by just right clicking and add new files. and select plist from others option.
Warrior
It doesn't change the fact that the resources directory is not writeable. If you have a plist that ships with the app, that you need to modify and then save again, you'll have to make a writable copy of that plist in the documents folder on the first run of the app, and use that version. This is the same approach used when shipping a default sqlite database within an app. When the app runs for the first time, copy the plist into the documents folder. Then for all your operations that use that plist, whether reading or writing, use the verison in the documents folder.
Jasarien