问题描述
- 应用报出了不兼容指针错误
- 正在读取UTF8编写的txt文件。
代码:
- (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];}
在最后一行报错: ** incompatible integer to pointer conversion sending int to parameter of type NSUTF8StringEncoding **
请高手指点,谢谢
解决方案
usedEncoding这个参数是用来传递调用方的返回值。
因此需要声明一个变量,传递关联给它
NSStringEncoding enc;NSString *result = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding: &enc error:nil];// Do something with the information about the encoding usedif ( enc == NSUTF8StringEncoding ) { // ...}return result;
时间: 2024-08-03 01:14:59