这是我读取文件内容的方法。不幸的是对我不起作用。文件的路径是正确的。我错过了什么?
- (IBAction)readFile:(id)sender
{
NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt"
encoding:NSUTF32StringEncoding
error:nil
];
NSLog(@"%@", str); //Result is null
}
答案 0 :(得分:2)
尝试使用stringWithContentsOfFile:encoding:error:
的内置功能并给它一个NSError
,你可以看到错误的错误:
- (IBAction)readFile:(id)sender {
NSError *readFileError = nil;
NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt"
encoding:NSUTF32StringEncoding
error:&readFileError
];
if (readFileError) {
NSLog(@"%@ - %@", [readFileError localizedDescription], [readFileError localizedFailureReason]);
} else {
NSLog(@"%@", str);
}
}