我试图逐行读取文本文件,而文本文件将是一个非常小的文件,因此我只使用了:
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];但是,在这一行中提出了一个例外,即:
[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x7f92c40e1890我对目标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 URL部分吗?谢谢。
发布于 2012-04-09 03:26:27
看起来filePath是一个NSURL,但是stringWithContentsOfFile:encoding:error:希望路径是一个NSString。
试试这个:
NSString *fileContents = [NSString stringWithContentsOfURL:filePath encoding:NSUTF8StringEncoding error:nil];发布于 2012-04-09 03:26:35
您将得到此错误,因为NSURL没有方法getFileSystemRespresentation,这是在NSString中。
您可以在fileContents字符串上使用此方法,也可以将fileContents字符串传递给NSURL的URLWithString方法。
https://stackoverflow.com/questions/10068544
复制相似问题