iphone - Plist 未从字典保存(保存到文档)

标签 iphone save plist nsdictionary documents

我一直在尝试将 NSDictionary 的 plist 保存到我的应用程序的 Documents 文件夹中。我还没有在设备上尝试过这个,但我希望它能够在模拟器上运行以进行测试。 [self createDictionaryFromChoreList] 方法只是根据我的另一个类中的一些数据创建一个 NSDictionary。我几乎从网络文档中复制/粘贴了这段代码,当我查看文件是否已保存时,我发现它没有保存。这是方法 block 。

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistName = [[NSString alloc] initWithFormat:@"%@chores.plist", self.firstName];
NSString *path = [documentsDirectory stringByAppendingPathComponent:plistName];

NSDictionary *choresDictionary = [[NSDictionary alloc] initWithDictionary:[self createDictionaryFromChoreList]];
[choresDictionary writeToFile:path atomically:YES];

非常感谢任何帮助。提前致谢。

-S

最佳答案

您还应该捕获 writeToFile:atomically: 返回的 BOOL 。这会告诉你写入是否成功。

此外,您确定您正在查找正确的文档文件夹吗?如果模拟器中有多个应用程序,则很容易在 Finder 中打开错误的应用程序文档文件夹。我曾经这样做过一次,这让我沮丧了几个小时。

编辑01:

writeToFile:atomically:返回 false 解释了为什么不存在文件。最简单的解释是字典中的某些东西不是属性列表对象。

来自 NSDictionary 文档:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

它只需将一个非 plist 对象深埋在字典中即可防止其转换​​为 plist。

关于iphone - Plist 未从字典保存(保存到文档),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2114444/

相关文章:

ios - 获取推送通知的设备 token

xcode Swift 将录制的视频保存为 mov 或 mp4

python - 终止时保存列表值

tensorflow - 无法在 TensorFlow 2 中加载模型权重

iOS)我应该在哪里复制和粘贴 facebook XML 片段?

iphone - 如何在plist中存储uint32_t

iphone - 如何读取 Plist 数据到 UITableView

iphone - UINavigationController popToRootViewController,然后立即推送一个新 View

iPhone - 根据传递给它的数据动态调整 UILabel 内容

iphone - 构建数组的数组