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

后端 未结 6 1855
闹比i
闹比i 2021-02-12 22:33

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         


        
6条回答
  •  感情败类
    2021-02-12 22:45

    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.

提交回复
热议问题