tags:

views:

427

answers:

1

hi all

does anybody know how to add a child key to the root key programmatically in a plist?

sean

+2  A: 
 NSString *path = @"path/to/some/file";
 NSMutableDictionary *plist = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];
 [plist setObject:@"foo" forKey:@"aKey"];
 [plist writeToFile:path atomically:YES];
 [plist release];

Note that you cannot write inside your own bundle; you'll have to copy the file to somewhere within your app's sandbox. Check out the NSSearchPathForDirectoriesInDomains function.

Noah Witherspoon
Hi, thanks for your code example but I can't get it work this way. If I try like you described in your example it just sets the value for the key, but I'm trying to add a sub key (add an sub key (array) named movies to the first key, which is generally the root key).
Sean
ok, i solved my problem. instead of trying to create a key programmatically in a plist file which is located in the application documents directory, I created a plist file with the key manually in the resource folder and then i copy this file to the app documents directory.
Sean