ios - 向 Journal 的 iOS plist 添加多个词典

标签 ios objective-c dictionary nsdictionary plist

我有一个应用程序,我想在应用程序的整个生命周期中存储多条信息。为此,我制作了一个名为“myJournal.plist”的 plist 文件。一开始 plist 中不会有任何内容,因为它将完全取决于用户的操作。我希望 plist 看起来像这样,其中每个词典都基于当天并添加到列表中。

enter image description here

我的代码中有以下内容:

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];

但是什么都没有改变。我做错了什么?

最佳答案

  1. 您可能第一次尝试写入尚未创建或为空的文件,因此您的 loadedMiscDictionarynil。结果,最后您尝试将 nil 写入文件。

  2. bundle 中的文件(我假设在屏幕截图上)与 Documents 目录中的文件不同。启用文档共享并在 iTunes 中检查您的应用程序的文档目录。我想您会在那里看到实际更改的文件。

关于ios - 向 Journal 的 iOS plist 添加多个词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646980/

相关文章:

java - 如何使用 "US-ASCII"和 HmacSHA256 将 java 代码覆盖到 Objective-C ?

ios - 通过 POST 登录后如何到达下一页

ios - 将 “correct” 数据从 UITableViewController 中的选定单元格传递到 ViewController

ios - Keep Alive 在 iOS 上无法正常工作

iphone - iOS UIScrollView : Cannot scroll until the end of the content

ios - 有没有更好的方法来获取内容大小

swift - 在 Swift 中单击按钮将字典的项目逐一显示到标签

python - 如何从 Python 字典中删除键?

c# - 你如何按值对字典进行排序?

ios - 延迟后如何切换回原始按钮图像(obj-c)?