NSString stringWithContentsOfFile失败,似乎是错误的错误代码 [英] NSString stringWithContentsOfFile failing with what seems to be the wrong error code

查看:178
本文介绍了NSString stringWithContentsOfFile失败,似乎是错误的错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件加载到字符串中。这是我正在使用的代码:

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];

当传入@about作为文件名时,它完全正常,显示代码有效。
当传入@eula作为文件名时,它以Cocoa error 258失败,转换为NSFileReadInvalidFileNameError。但是,如果我交换文件的内容但保持名称相同,则另一个文件无法证明文件名没有任何问题,这与内容有关。

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文件是相当简单的HTML,但eula文件是由法律部门从Word导出的大量混乱。

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

有没有人知道HTML中的任何内容可能导致此错误的文件?

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

非常感谢,

Sam

推荐答案

错误几乎肯定是你的文件不是UTF-8,但是你是对的,这听起来像错误报告中的错误。

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.

用BBEdit(或免费的TextWrangler)打开eula文件,看看它使用的是什么编码。将编码更改为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).

如果这样可以解决问题,那么使用 Apple Bug Reporter 报告错误报告中的错误。

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

这篇关于NSString stringWithContentsOfFile失败,似乎是错误的错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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