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?

Community
  • 1
  • 1
Ashley Staggs
  • 1,557
  • 9
  • 24
  • 38

3 Answers3

25

Here's the simplest way:

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

See the documentation for -writeToFile:atomically:.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 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->string – Ashley Staggs Jun 10 '11 at 19:41
  • 6
    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. – John Calsbeek Jun 10 '11 at 19:43
  • 2
    **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. – enadun Aug 14 '16 at 13:59
  • This method is deprecated. – Denis Kutlubaev Nov 03 '17 at 13:53
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];
Krishna Shanbhag
  • 1,519
  • 13
  • 33
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • i know where i am going to save it, but it's the forming that i need to get right – Ashley Staggs Jun 10 '11 at 19:42
  • 2
    @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. –  Jun 10 '11 at 21:23
  • 3
    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. – Rob Keniger Jun 11 '11 at 01:14
  • @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?? – temporary_user_name Dec 14 '14 at 09:22
  • You should store your plist in any of the [various places Apple provides for you to store things](https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW4). – Rob Keniger Dec 15 '14 at 06:45
0

And conversely, to load a NSDictionary FROM a file:

+ (id)dictionaryWithContentsOfFile:(NSString *)path;
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
pkasson
  • 37
  • 5