Save NSDictionary to plist

后端 未结 3 1968
梦如初夏
梦如初夏 2020-11-27 17:30

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 cr

相关标签:
3条回答
  • 2020-11-27 17:40

    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];
    
    0 讨论(0)
  • 2020-11-27 17:42

    And conversely, to load a NSDictionary FROM a file:

    + (id)dictionaryWithContentsOfFile:(NSString *)path;
    
    0 讨论(0)
  • 2020-11-27 17:56

    Here's the simplest way:

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

    See the documentation for -writeToFile:atomically:.

    0 讨论(0)
提交回复
热议问题