正在讀取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 used
if ( enc == NSUTF8StringEncoding ) {
// ...
}
return result;