加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 从XCode Bundle读取文本文件

发布时间:2020-12-14 18:03:01 所属栏目:百科 来源:网络整理
导读:为什么我不能读取foo.rtf的内容?我已经把它放在XCode包中了. fileRoot仍然包含null. NSString* filePath = @"foo";NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"rtf"]; NSLog(@"File root contains %@",fileRoot); 感谢
为什么我不能读取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. (我不能插入图片,因为我还是新手).

(我可以在fileRoot指向正确后添加内容读取).

解决方法

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

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的内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读