无法在 XCODE 4.2 中读取文件,但在 4.0 中可以工作?
我已经从 XCODE 4 升级到 4.2,现在遇到了问题。
以下代码在 4.2 之前工作,用于读取“filePath”中的文件:
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);
在 4.2 中,以下代码行中的“initWithContentsOfFile”已被弃用:
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
...并且应该根据手册替换为以下内容:
NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];
并且我无法得到这个通过替换代码行来读取同一文件中的记录。顺便说一句,我已经定义了 &err。
当我 NSLog myString 时,我得到(null)。
我有点迫切地想解决这个问题,非常感谢任何帮助。
干杯
I have upgraded from XCODE 4 to 4.2 and now i have problems.
The following code worked pre 4.2 to read the file in "filePath":
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);
With 4.2 the "initWithContentsOfFile" in the following code line is deprecated:
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
...and should be replaced with the below according to the manual:
NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];
and i can not get this to read the records in the same file by replacing the code line. BTW, i have defined the &err.
When i NSLog myString i get (null).
I am getting a bit desperate to solve this and would very much appreciate any help.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSLog
err
变量(如果有错误)。还有NSLog
filePath
。也许编码不是 UTF-8,您确定编码吗?
最好的非 UTF-8 编码是
NSMacOSRomanStringEncoding
,它支持 8 位字符。NSLog
theerr
variable if there is an error. AlsoNSLog
filePath
.Perhaps the encoding is not UTF-8, are you sure about the encoding?
The best non-UTF-8 encoding bet is
NSMacOSRomanStringEncoding
which supports 8-bit characters.尝试:
如果这不起作用,请尝试使用您的代码:
NSASCIIStringEncoding
Try :
If that does not works, try in your code :
NSASCIIStringEncoding
该文件可能不包含 UTF8 编码的字符串。请参阅苹果的文档,其中有一个读取不知道编码的文件的示例: 使用未知编码读取数据
您需要使用
[string|init]WithContentsOfFile:usedEncoding:error
方法,如果失败,在最终向用户呈现错误消息之前,您还可以尝试其他一些操作(例如,尝试将其读取为NSAttributedString
)。例如,您可以这样做:
The file probably doesn't contain a UTF8 encoded string. See apple's documentation, which has an example of reading a file where you do not know the encoding: Reading data with an unknown encoding
You need to use the
[string|init]WithContentsOfFile:usedEncoding:error
method, and if that fails there are a few more things you can try before finally presenting an error message to the user (for example, try reading it as anNSAttributedString
).For example, you could do this: