Objective c Can';不读取文件
下面是我读取文件内容的方法。不幸的是,这对我不起作用。文件的路径是正确的。我想念什么Objective c Can';不读取文件,objective-c,xcode,macos,Objective C,Xcode,Macos,下面是我读取文件内容的方法。不幸的是,这对我不起作用。文件的路径是正确的。我想念什么 - (IBAction)readFile:(id)sender { NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt" encoding:NSUTF32StringEncoding error:nil
- (IBAction)readFile:(id)sender
{
NSString *str = [NSString stringWithContentsOfFile:@"/Users/joe/text.txt"
encoding:NSUTF32StringEncoding
error:nil
];
NSLog(@"%@", str); //Result is null
}
尝试使用
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);
}
}
没有NSPath类吗?嗯..我想知道stringWithContentsOfFile:encoding:error:的错误参数是什么?@Legolas,你说的Npath Joseph是什么意思?我不确定。@user979009:Joseph的意思是你应该向错误参数传递一个值,并在尝试读取文件时实际检查错误是什么。(或者更确切地说,不是一个错误,而是一个地址引用。)@Inerdia我明白了!谢谢问题在于编码参数。