ios NSString stringWithContentsOfFile 失败,错误代码似乎是错误的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/999043/
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
NSString stringWithContentsOfFile failing with what seems to be the wrong error code
提问by deanWombourne
I'm trying to load a file into a string. Here is the code I'm using:
我正在尝试将文件加载到字符串中。这是我正在使用的代码:
NSError *error = nil;
NSString *fullPath = [[NSBundle mainBundle] pathForResource:filename
ofType:@"html"];
NSString *text = [NSString stringWithContentsOfFile:fullPath
encoding:NSUTF8StringEncoding
error:&error];
When passed in @"about" as the filename, it works absolutely fine, showing the code works. When passed in @"eula" as the filename, it fails with 'Cocoa error 258', which translates to NSFileReadInvalidFileNameError. However, if I swap the contents of the files over but keep the names the same, the other file fails proving there is nothing wrong with the filename, it's something to do with the content.
当 @"about" 作为文件名传入时,它工作得很好,显示代码有效。当@"eula" 作为文件名传入时,它失败并返回“Cocoa 错误 258”,转换为 NSFileReadInvalidFileNameError。但是,如果我交换文件的内容但保持名称相同,则另一个文件无法证明文件名没有问题,这与内容有关。
The about file is fairly simple HTML but the eula file is a massive mess exported from Word by the legal department.
about 文件是相当简单的 HTML,但 eula 文件是法律部门从 Word 导出的一大堆乱七八糟的文件。
Does anyone know of anything inside a HTML file that could cause this error to be raised?
有谁知道 HTML 文件中可能导致引发此错误的任何内容?
Much thanks,
非常感谢,
Sam
山姆
采纳答案by Peter N Lewis
The error is almost certainly that your file is not in UTF-8, but you're right, that does sound like a bug in the error report.
错误几乎可以肯定是您的文件不是 UTF-8,但您是对的,这听起来确实像是错误报告中的错误。
Open the eula file up with BBEdit (or the free TextWrangler) and see what encoding it uses. Change the encoding to UTF-8 and save it. Diff the two files to see what differences have appeared. Replace the original file with the new one (fixing any glitches).
使用 BBEdit(或免费的 TextWrangler)打开 eula 文件并查看它使用的编码。将编码更改为 UTF-8 并保存。比较这两个文件以查看出现了哪些差异。用新文件替换原始文件(修复任何故障)。
If that resolves the problem, then use the Apple Bug Reporterto report the bug in the error report.
如果这解决了问题,则使用Apple Bug Reporter在错误报告中报告错误。
回答by Eli
I've just spent 45 minutes with this problem, only in my case the solution was stupidand the problem slightly different.
我刚刚在这个问题上花了 45 分钟,只有在我的情况下,解决方案很愚蠢,而且问题略有不同。
- I had a file called Playlist.txt in my resources directory. It was loading just fine.
- The file was modified at one point, from within XCode.
- The file stopped loading properly, with the same error as above. However, it had never been moved nor had its encoding type been changed.
- I did a command-I (Get Info) on the file in the XCode directory, it told me it was UTF-8 (as expected).
- I tried the "usedEncoding" way of reading files, no dice. Same error, encoding was return null.
- Finally, I erased the file from XCode, dragged it in again, and did a Clean All. That fixed the problem.
- 我的资源目录中有一个名为 Playlist.txt 的文件。它加载得很好。
- 该文件曾在 XCode 中被修改过。
- 该文件停止正确加载,并出现与上述相同的错误。但是,它从未被移动过,其编码类型也从未改变过。
- 我在 XCode 目录中的文件上执行了一个命令-I(获取信息),它告诉我它是 UTF-8(正如预期的那样)。
- 我尝试了读取文件的“usedEncoding”方式,没有骰子。同样的错误,编码返回空值。
- 最后,我从 XCode 中删除了该文件,再次将其拖入,然后进行了一次 Clean All。这解决了问题。
This is not the first time that XCode magically caching things (incorrectly) has caused me hours and hours of wasted time. If you have an error like this which doesn't make sense, try removing and replacing files and cleaning all targets.
这不是 XCode 神奇地缓存内容(错误地)第一次让我浪费时间。如果您有类似这样的错误,请尝试删除和替换文件并清理所有目标。
回答by baowuwolf
I had the same error with you ,use file name with [[NSBundle mainBundle] pathForResource:@"pageList" ofType:@"txt"]] good luck!
我和你有同样的错误,使用文件名 [[NSBundle mainBundle] pathForResource:@"pageList" ofType:@"txt"]] 祝你好运!
回答by Naaff
The most likely reason that +stringWithContentsOfFile:encoding:error:
would fail in this case would be if you provided the wrong encoding. Are you sure that your @"eula"
file is UTF8 encoded?
+stringWithContentsOfFile:encoding:error:
在这种情况下失败的最可能原因是您提供了错误的编码。你确定你的@"eula"
文件是UTF8编码的吗?
If you're unsure about the encoding of the file, you could always try +stringWithContentsOfFile:usedEncoding:error:
instead and see if it works and what encoding it comes up with.
如果您不确定文件的编码,您可以随时尝试+stringWithContentsOfFile:usedEncoding:error:
查看它是否有效以及它提供了什么编码。
回答by Olie
Don't know if this is your problem, but I just had a similar thing (stringWithContentsOfFile, no JSON), and the problem was that the file had CRLF (windows) line-endings and Western-whatever-it's-called encoding. I used SubEthaEdit to convert to LF and UTF-8, and everything works fine.
不知道这是否是您的问题,但我刚刚遇到了类似的问题(stringWithContentsOfFile,没有 JSON),问题是该文件具有 CRLF(windows)行尾和西方任何所谓的编码。我使用 SubEthaEdit 转换为 LF 和 UTF-8,一切正常。
回答by Shailesh
I ran into the same error.
我遇到了同样的错误。
When I played around a bit, it appeared that I was not including the file in copy bundle resources of the target.
当我玩了一下时,似乎我没有将文件包含在目标的复制包资源中。
I did that and it worked fine.
我这样做了,而且效果很好。
But, have to say -- error is quite misleading for such a simple cause. Just a bad guess from Xcode
但是,不得不说——对于这样一个简单的原因,错误是相当具有误导性的。来自 Xcode 的错误猜测