0

I am using this simple method I found to write to a plist but it doesn't seem to work:

-(IBAction)modifyPlist:(id)sender{

NSBundle *mainBundle=[NSBundle mainBundle];
NSString *path=[mainBundle pathForResource:@"Preferences" ofType:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[[dict objectForKey:@"Root"] setBool:YES forKey:@"startup"];
}

What am I doing wrong? Thanks

Tom Hobson
  • 23
  • 6

3 Answers3

1

You're loading the file into memory, and modifying that memory, but you're not writing it back to disk. You'll want to use NSDictionary's -writeToFile:atomically:

[dict writeToFile:path atomically:YES];

Reference link here.

zpasternack
  • 17,838
  • 2
  • 63
  • 81
0

I hope you know exactly what you're doing and this is (for whatever reason) your design decision -
because you're about to write to your app's bundle.

That'll certainly fail under Sandboxing and is generally a bad, bad idea.

If you've after storing user defaults please read up on the Mac way of storing preferences:
Preferences and Settings Programming Guide

cacau
  • 3,606
  • 3
  • 21
  • 42
0
// Make a path to the plist in the Documents directory (not the bundle directory)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Preferences.plist"];

// Then after creating or making changes to your dictionary, write your dictionary out to the plist file
[dict writeToFile:path atomically:YES];

Maybe look at: Should I use NSUserDefaults or a plist to store data?

Community
  • 1
  • 1
iCMS
  • 489
  • 4
  • 9