我有一个具有以下格式的 plist:
我是这样读的:
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"];
问题是如果我想将这个名为 self.statesData 的 NSMutableArray 写回到 plist 中,我该怎么做?我猜这样的事情是行不通的......
NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];
[self.main.statesData writeToFile:path atomically: YES];
您不能写入应用程序的捆绑包,您需要写入应用程序的文档目录。
示例代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
BOOL status = [array writeToFile:filePath atomically:YES];
如果您在第一次运行时应用程序包中有初始爸爸,请将其复制到文档目录并进行对文档目录的所有进一步访问。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)