【发布时间】:2011-10-24 12:00:31
【问题描述】:
所以我已经阅读了大量关于 plist 以及如何保存到它们的 SO-questions,虽然我不知道,但为什么到目前为止我看到的 iPhone-Dev-Book 没有涵盖这个(他们都使用tableView 编辑功能),我设法通过将其复制到文档文件夹中来真正写入 plist,如下所示:
pListPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
pListPath = [pListPath stringByAppendingPathComponent:@"piggyBanks.plist"];
NSLog(@"Path: %@", pListPath);
// if the file does not exist yet, create it, and copy the plist data into it, that can be found in the bundle
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:pListPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"piggyBanks" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:pListPath error:nil];
}
// make the plist content available for usage
pListContent = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];
NSLog(@"pListContent: %@", pListContent);
到目前为止一切都很好,但是现在,如果我想在用户点击 tableViewCell 时更改一些 plist 值(如果这很重要,这是一个自定义值),尽管 pListPath、pListContent 和其他属性是定义的在 .h 中并在 .m 中合成,我必须在 didSelectRowAtIndexPath 中重新定义 pListPath 和 pListContent,以便在那一刻知道路径。
谁能告诉我为什么?我的意思是,这很好,它有效,但我想知道,为什么它必须这样,或者我是否在其他地方犯了错误..
谢谢!
【问题讨论】:
标签: iphone objective-c plist nsbundle