我正在尝试逐行读取文本文件,并且该文本文件将是一个非常小的文件,所以我只是使用:
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
但是,该行出现了一个异常,表示:
[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x7f92c40e1890
我对 Objective-C 真的很陌生,我不明白为什么会发生这种情况......
提前致谢。
<小时/>NSString *filePath;
NSOpenPanel *fileBrowser = [NSOpenPanel openPanel];
[fileBrowser setCanChooseFiles:YES];
[fileBrowser setCanChooseDirectories:YES];
if ([fileBrowser runModal] == NSOKButton) {
NSArray *files = [fileBrowser URLs];
for ( int i = 0; i < [files count]; i++ ) {
filePath = [files objectAtIndex:i];
}
}
这是因为 [fileBrowser URLs] 部分吗? 谢谢。
最佳答案
看起来filePath
是一个NSURL
,但是stringWithContentsOfFile:encoding:error:
期望路径是一个NSString
.
试试这个:
NSString *fileContents = [NSString stringWithContentsOfURL:filePath encoding:NSUTF8StringEncoding error:nil];
关于objective-c - Objective C 文件读取过程中出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068544/