objective-c - 从 Xcode Bundle 读取文本文件

标签 objective-c xcode macos file bundle

为什么我看不到foo.rtf的内容?我已经把它放在 Xcode 包中了。 fileRoot 仍然包含 null。

NSString* filePath = @"foo";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"];       

NSLog(@"File root contains %@", fileRoot);

@TheAmateurProgrammer,谢谢你的图表。我刚刚将 foo.rtf 添加到 bundle 资源中。 fileRoot 的结果仍然为空。我还缺少哪一步?

Target 在构建阶段中的“复制 bundle 资源”现在包含 foo.rtf。 (我还是新手,不能插入图片)。

(等我可以让fileRoot正确指向后,再添加内容阅读)。

最佳答案

您添加了文件,但它真的复制到您的应用程序包中了吗?

确保您的 rtf 文件已复制到您的资源中。

其次,您只能获取 rtf 的路径,而不是 rtf 的内容。

NSString *fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"]; 
NSString *contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];

这将获取 rtf 的内容。

关于objective-c - 从 Xcode Bundle 读取文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995661/

相关文章:

ios - 如何使用 Repl Xcode 7?获取绝对路径错误

ios - 如何管理被禁用/启用的按钮

macos - 升级到osx优胜美地后Nginx损坏

objective-c - NSTextField 和 FirstResponder

ios - IAP 恢复托管内容

ios - 尝试在用户选择时检索 UITableView 单元格的内容

ios - 当应用程序变为事件状态时,文件已加密或不是数据库消息

c++ - 代码 4 : file as command argument

c - mac 上的静态链接库

iphone - NSNotificationCenter 发送者平等标准