为什么我看不到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/