把NSArray写回plist [英] writing NSArray back to plist
本文介绍了把NSArray写回plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以下格式的plist:
I have a plist with the following format:
以下是我的阅读方式:
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,怎么会我做到了吗?我猜这样的事情不行......
The question is if I want to write this NSMutableArray called self.statesData back to the plist, how would I do it? 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.
示例代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
BOOL status = [array writeToFile:filePath atomically:YES];
如果您在第一次运行时在应用程序包中有初始爸爸,请将其复制到Documents目录并进行全部进一步访问Documents目录。
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.
这篇关于把NSArray写回plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文