Tom Hobson
Tom Hobson

Reputation: 23

Why does this not work? - Writing data to plist

I am using the following code to modify a plist but to no avail:

-(IBAction)modifyPlist:(id)sender{
NSBundle *mainBundle=[NSBundle mainBundle];
NSString *path=[mainBundle pathForResource:@"Preferences" ofType:@"plist"];
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[dict setValue:@"YES" forKey:@"test"];
[dict writeToFile:path atomically:YES];
}

Does anyone know why it does't change the plist value 'test' from NO to YES?

Upvotes: 0

Views: 30

Answers (2)

iCMS
iCMS

Reputation: 499

You are writing to your app's bundle directory. It's ok to read from a plist in that directory, but you aren't allowed to write to your own app's resources so that won't work. Instead you have to write to a plist in the Documents directory, which you can get the path to in a similar way:

// Find the path to the plist in the Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Preferences.plist"];

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

Long-story-short: because you cannot write into the app bundle.

Upvotes: 1

Related Questions