Plist未从字典保存(保存到文档) [英] Plist not saving from dictionary (to Documents)

查看:77
本文介绍了Plist未从字典保存(保存到文档)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将NSDictionary的plist保存到我的应用程序的Documents文件夹中.我尚未在设备上尝试过此操作,但我希望它可以在模拟器上进行测试. [self createDictionaryFromChoreList]方法只是根据另一类我的数据创建一个NSDictionary.我已经从Web文档中复制/粘贴了此代码,当我去查看文件是否已保存时,我发现不是.这是方法块.

I've been trying to save a plist of a NSDictionary to my app's Documents folder. I haven't tried this on the device yet but I'd like it to work on the simulator for testing purposes. The [self createDictionaryFromChoreList] method just creates a NSDictionary from some data in another class of mine. I've pretty much copied/pasted this code from the web documents and when I go to see if the file was saved or not, I find that it isn't. Here is the method 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];

任何帮助将不胜感激.预先感谢.

Any help is greatly appreciated. Thanks in advance.

-S

推荐答案

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

You should also capture the BOOL returned by writeToFile:atomically:. That will tell you if the write succeeded or not.

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

Also, are you sure you are looking in the right documents folder? If you have more than one app in the simulator its easy to open the wrong app's documents folder in the Finder. I did that once and it cost me a couple of hours of frustration.

Edit01:

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

writeToFile:atomically: returning false explains why no file exist. The simplest explanation is that something in the dictionary is not a property list object.

来自NSDictionary文档:

From the NSDictionary docs:

此方法递归验证 所有包含的对象都是属性 列出对象(NSData的实例, NSDate,NSNumber,NSString,NSArray, 或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.

It just takes one non-plist object buried deep in a dictionary to prevent it from being converted to a plist.

这篇关于Plist未从字典保存(保存到文档)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆