NSString stringWithContentsOfFile failing with what seems to be the wrong error code

泪湿孤枕 提交于 2019-12-14 00:17:17

问题


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.

The about file is fairly simple HTML but the eula file is a massive mess exported from Word by the legal department.

Does anyone know of anything inside a HTML file that could cause this error to be raised?

Much thanks,

Sam


回答1:


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.

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).

If that resolves the problem, then use the Apple Bug Reporter to report the bug in the error report.




回答2:


I've just spent 45 minutes with this problem, only in my case the solution was stupid and the problem slightly different.

  • 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.

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.




回答3:


I had the same error with you ,use file name with [[NSBundle mainBundle] pathForResource:@"pageList" ofType:@"txt"]] good luck!




回答4:


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?

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.




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/999043/nsstring-stringwithcontentsoffile-failing-with-what-seems-to-be-the-wrong-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!