Save NSDictionary to plist

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.

  • Plist Array to NSDictionary
  • How to populate a TableView with plist content using XCode and 4 storyboard
  • Editing Info.plist possible programmatically?
  • Setting up a plist to store application data (not settings) for an iPhone game
  • Save Data to .plist File in Swift
  • Replace occurrences of NSNull in nested NSDictionary
  • 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?

    3 Solutions Collect From Internet About “Save NSDictionary to plist”

    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];
    

    Here’s the simplest way:

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

    See the documentation for -writeToFile:atomically:.

    And conversely, to load a NSDictionary FROM a file:

    + (id)dictionaryWithContentsOfFile:(NSString *)path;