不兼容的整数到指针转换?什么? [英] Incompatible integer to pointer conversion? what?
问题描述
我正在读取一个用UTF8编码的txt文件。
I am reading a txt file that was encoded with UTF8.
我正在读取该文件。我有这样的行:
I am now reading the file. I have this lines:
- (NSString *) readFromFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
documentsDirectory];
return [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:NSUTF8StringEncoding
error:nil];
}
我在最后一行有这个错误:
I have this error on the last line:
与指针转换不兼容的整数将int发送到NSUTF8StringEncoding类型的参数*
为什么?
推荐答案
这里的关键是使用 d 编码中的d。
The key here is the "d" in "usedEncoding".
该参数用于将信息传递回调用者关于使用的编码以读取文件。
That parameter serves to pass information back to the caller about the encoding used to read the file.
这意味着它是 NSString
类,它通知调用者它用于创建对象的编码。文件中的原始数据 - 不可以指示班级使用哪种编码的调用者。
This means that it is the NSString
class that informs the caller which encoding it used to create the object from the raw data in the file - and not the caller that can instruct the class about which encoding to employ.
要获取此信息,您需要声明变量并传递对它的引用,以便 NSString
类对象可以将信息放入其中,以便稍后在需要时阅读。
To get this information, you need to declare a variable and pass a reference to it so the NSString
class object can put the information into it for you to read later if you need the information.
NSStringEncoding enc;
NSString *result = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding: &enc
error:nil];
// Do something with the information about the encoding used
if ( enc == NSUTF8StringEncoding ) {
// ...
}
return result;
这篇关于不兼容的整数到指针转换?什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!