为什么我不能读取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的结果仍为null.我还缺什么步骤?
Build Phases中Target的“Copy Bundle Resources”现在包含foo.rtf. (我不能插入图片,因为我还是新手).
解决方法
您添加了该文件,但它是否真的已复制到您的应用程序包中?
http://f.cl.ly/items/1d3y2d3b101C0g07462N/Screen%20Shot%202012-10-21%20at%203.08.44%20PM.png
确保将rtf文件复制到资源中.
其次,你只得到rtf的路径,而不是rtf的内容.
NSString *fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"]; NSString *contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];
这将获得rtf的内容.