objective-c - Objective C 文件读取过程中出现异常?

标签 objective-c xcode cocoa exception osx-lion

我正在尝试逐行读取文本文件,并且该文本文件将是一个非常小的文件,所以我只是使用:

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/

相关文章:

iphone - 从iphone实时发送音频到服务器?

objective-c - 符号化 Cocoa Mac 应用程序崩溃日志

objective-c - UITableView 处于编辑模式时可选择的 UITableViewCells

objective-c - Xcode 4.4 + OSX 10.5 作为开发目标 = Clang LLVM 1.0 错误

xcode - 如何在 Xcode lldb 中打印超过 256 个数组元素?

cocoa - 如何打开文件夹?

iphone - 您可以在 IB 中建立一个按钮并以编程方式移动它吗?

xcode - 如何使用 AppleScript 在 XCode 中创建新项目?

cocoa - NSCollectionView 中项目的索引

cocoa - 在用户默认设置中存储语音 - Cocoa