Save NSDictionary to plist
Asked Answered
P

3

15

I am using the code found in this post: Mulitple Arrays From Plist, with the same plist formatting.

This works successfully, however I do not know how to save the created arrays back into a plist, in the same format.

How would I achieve this?

EDIT: It is not so much the saving that I need, but the forming of the data to save.
The plist is an array of dictionaries with strings and their corresponding keys. All the strings with a certain key are put into an array of their own.

How would I put that array back into the correct positions in the array of dictionaries, ready to save?

Pocketknife answered 10/6, 2011 at 19:13 Comment(0)
K
17

Don't forget to create myPlistFile.plist and add it in your application resource folder.

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

you could scan paths here and search myPlistFile.plist using for loop.

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath: plistPath])
{
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@"myPlistFile" ofType:@"plist"];
          [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
}
[myDict writeToFile:plistPath atomically: YES];
Kastroprauxel answered 10/6, 2011 at 19:22 Comment(5)
i know where i am going to save it, but it's the forming that i need to get rightPocketknife
@Ashley Technically, bundles are read-only for App Store-targeted applications only. If that’s not the case, an application can write to its bundle.Stupor
It can, but only if the user has admin privileges and the app is not code signed. It's considered bad practice to modify the app bundle.Algonquian
@RobKeniger wait, I didn't know this....so I was planning on writing to a plist in the "main bundle," if that's what it's called....so what should I do instead??Scruff
You should store your plist in any of the various places Apple provides for you to store things.Algonquian
G
25

Here's the simplest way:

NSDictionary* dict = ...;
[dict writeToFile:@"..." atomically:YES];

See the documentation for -writeToFile:atomically:.

Guadalquivir answered 10/6, 2011 at 19:21 Comment(4)
I know that this will save the file, but i don't think this will turn the arrays formed from the Plist back into the same way of array->dictionary->stringPocketknife
Have you tried it? It can write to a plist anything that can be read from a plist. Unless you've changed the way you order the data in the dictionary, it should write out in exactly the same way.Guadalquivir
Most important - This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.Brazier
This method is deprecated.Christiechristin
K
17

Don't forget to create myPlistFile.plist and add it in your application resource folder.

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

you could scan paths here and search myPlistFile.plist using for loop.

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath: plistPath])
{
          NSString *bundle = [[NSBundle mainBundle] pathForResource:@"myPlistFile" ofType:@"plist"];
          [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
}
[myDict writeToFile:plistPath atomically: YES];
Kastroprauxel answered 10/6, 2011 at 19:22 Comment(5)
i know where i am going to save it, but it's the forming that i need to get rightPocketknife
@Ashley Technically, bundles are read-only for App Store-targeted applications only. If that’s not the case, an application can write to its bundle.Stupor
It can, but only if the user has admin privileges and the app is not code signed. It's considered bad practice to modify the app bundle.Algonquian
@RobKeniger wait, I didn't know this....so I was planning on writing to a plist in the "main bundle," if that's what it's called....so what should I do instead??Scruff
You should store your plist in any of the various places Apple provides for you to store things.Algonquian
E
0

And conversely, to load a NSDictionary FROM a file:

+ (id)dictionaryWithContentsOfFile:(NSString *)path;
Ellette answered 23/5, 2013 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.