我有一个应用程序,我想在应用程序的整个生命周期中存储多条信息。为此,我制作了一个名为“myJournal.plist”的 plist 文件。一开始 plist 中不会有任何内容,因为它将完全取决于用户的操作。我希望 plist 看起来像这样,其中每个词典都基于当天并添加到列表中。
我的代码中有以下内容:
NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [directories firstObject];
NSString *filename = [documents stringByAppendingPathComponent:@"myJournal.plist"];
NSMutableDictionary *loadedMiscDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:distractions.text forKey:@"distractions"];
[tempDict setValue:revelations.text forKey:@"revelations"];
[loadedMiscDictionary addEntriesFromDictionary:tempDict];
[loadedMiscDictionary writeToFile:filename atomically:YES];
但是什么都没有改变。我做错了什么?
最佳答案
您可能第一次尝试写入尚未创建或为空的文件,因此您的
loadedMiscDictionary
为nil
。结果,最后您尝试将nil
写入文件。bundle 中的文件(我假设在屏幕截图上)与 Documents 目录中的文件不同。启用文档共享并在 iTunes 中检查您的应用程序的文档目录。我想您会在那里看到实际更改的文件。
关于ios - 向 Journal 的 iOS plist 添加多个词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646980/