简体   繁体   English

把NSArray写回plist

[英]writing NSArray back to plist

I have a plist with the following format: 我有一个以下格式的plist:

在此输入图像描述

And here's how I read it: 以下是我的阅读方式:

NSString *errorDesc = nil;
    NSPropertyListFormat format;

    NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];    
    NSData  *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                     
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];

    self.statesData = [temp objectForKey:@"Root"];

The question is if I want to write this NSMutableArray called self.statesData back to the plist, how would I do it? 问题是,如果我想将这个名为self.statesData的NSMutableArray写回plist,我该怎么做? I am guessing something like this wouldn't work... 我猜这样的事情不行......

 NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];  
    [self.main.statesData writeToFile:path atomically: YES];

You can not write the app's bundle, you need to write to the app's Document directory. 您无法编写应用程序包,您需要写入应用程序的文档目录。

Sample code: 示例代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

BOOL status = [array writeToFile:filePath atomically:YES];

If you have initial dad in the app bundle on first run copy it to the Documents directory and make all further accesses to the Documents directory. 如果您在第一次运行时在应用程序包中有初始爸爸,请将其复制到Documents目录,并进一步访问Documents目录。

On iOS, you can't write to files in your app wrapper (and on MacOS X, you really shouldn't). 在iOS上,您无法写入应用包装器中的文件(在MacOS X上,您真的不应该)。 What you need to do is first copy to the Documents folder from the app wrapper, then load and save to that version. 您需要做的是首先从应用程序包装器复制到Documents文件夹,然后加载并保存到该版本。

Getting the Documents folder: 获取文档文件夹:

NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
;
filePath = [filePath stringByAppendingPathComponent:@"States.plist"];
[self.statesData writeToFile:path atomically:YES];

Loading the file from the app wrapper you are already doing; 从你正在做的app包装器加载文件; copying can be done readily using NSFileManager . 使用NSFileManager可以轻松完成复制。 You can reconstitute the temp array and replace the key from your statesData before saving. 在保存之前,您可以重新构建临时数组并从statesData中替换该键。 An alternate read command might be: 备用读取命令可能是:

self.statesData = [NSArray arrayWithContentsOfFile:filePath];

SWIFT version of @zaph's code above: 以上@ zaph代码的SWIFT版本:

let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory = paths[0]
let filePath = NSURL(fileURLWithPath: documentsDirectory, isDirectory: true).URLByAppendingPathComponent(fileName)
let status = items.writeToFile(filePath.absoluteString, atomically: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM