xcode writeToFile 失败 - 如何调试失败的原因(使用什么工具)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7519996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writeToFile fails - how do I debug WHY it fails (what tools)?
提问by MM.
I'm getting an NSDictionary from JSON (using SBJson), and I want to store it. I'm using
我从 JSON 获取 NSDictionary(使用 SBJson),我想存储它。我正在使用
[liveData writeToFile:localFilePath atomically:YES]
and it fails. The data lookslike its all NSString, NSDictionary, and NSArray (which "atomically:YES" demands). I used the same localFilePath elesewhere.
它失败了。数据看起来像它所有的 NSString、NSDictionary 和 NSArray(“原子:YES”要求)。我在其他地方使用了相同的 localFilePath。
So my question is: how can I find out where the problem is? What tools can I use to understand why writeToFile fails? The log doesn't show an error message.
所以我的问题是:我怎样才能找出问题所在?我可以使用哪些工具来理解 writeToFile 失败的原因?日志不显示错误消息。
回答by AliSoftware
It may have multiple reasons:
它可能有多种原因:
- The path you are writing to is wrong, not writable (you don't have write access to it), or the parent directory does not exists (if
localFilePath
is "/path/to/file.plist" but the directory "/path/to/" does not exists, it will fail) - The liveData dictionary does contains objects that are not PropertyList objects. Only
NSData
,NSDate
,NSNumber
,NSString
,NSArray
, orNSDictionary
objects can be written into a Property List file (andwriteToFile:atomically:
dowrite to a plist file so the dictionary dohave to contains only PList objects for that method to succeed)
- 您写入的路径错误,不可写(您没有写入权限),或者父目录不存在(如果
localFilePath
是“/path/to/file.plist”但目录“/path/ to/" 不存在,会失败) - liveData 字典确实包含不是 PropertyList 对象的对象。只有
NSData
,NSDate
,NSNumber
,NSString
,NSArray
,或NSDictionary
对象可以被写入一个属性列表文件(并writeToFile:atomically:
做写入plist文件中,因此字典也有只包含plist中的对象为这个方法成功)
回答by badweasel
I know this is a 2 year old question. But since I just had this same problem and fixed it here's what I found. I bet your NSDictionary has some keys that are not NSStrings.
我知道这是一个 2 岁的问题。但是因为我刚刚遇到了同样的问题并在这里解决了我发现的问题。我敢打赌你的 NSDictionary 有一些不是 NSStrings 的键。
Instead of keying like:
而不是像这样键入:
[_myDictionay setObject:thisObj forKey:[NSNumber numberWithInt:keyNumber]];
Key like:
关键像:
[_myDictionay setObject:thisObj forKey:[NSString stringWithFormat:@"%i",numberWithInt:keyNumber]];
That fixed my problem right up.
那解决了我的问题。
The top way can't be saved to a plist file.
顶级方式无法保存到 plist 文件。
Since you're getting your info from a JSON conversion, it is possible that there are some objects or keys that are NSNumbers in there. You would have to convert them. But that's a pain.
由于您是从 JSON 转换中获取信息,因此其中可能有一些对象或键是 NSNumbers。你必须转换它们。但这是一种痛苦。
So since you already have it in json, just store it as the json string in its entirety in a @"data" key and the re-expand it when you load the plist later back into your array or dict.
因此,由于您已经在 json 中拥有它,只需将其作为 json 字符串完整存储在 @"data" 键中,然后在将 plist 加载回数组或 dict 时重新展开它。
回答by matrinox
I've tried saving an NSDictionary to disk with only numbers for keys and values. Switching the keys to NSString works but not when they keys are NSNumber. Do keys have to be NSString?
我尝试将 NSDictionary 保存到磁盘,其中只有键和值的数字。将键切换为 NSString 有效,但当键为 NSNumber 时无效。键必须是 NSString 吗?
EDIT: I know better now that it can be any object that responds to hash; often it's NSString's, though.
编辑:我现在更清楚它可以是响应哈希的任何对象;不过,通常是 NSString 的。