从XCode包中读取文本文件 [英] Reading Text File from XCode Bundle

查看:406
本文介绍了从XCode包中读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能读取foo.rtf的内容?我已经把它放在XCode包中。 fileRoot仍然包含null。

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

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

感谢任何人的帮助。






@TheAmateurProgrammer,谢谢你的图。我只是添加foo.rtf到束资源。 fileRoot的结果仍然为空。我还缺少什么步骤?

Target构建阶段中的Copy Bundle Resources现在包含foo.rtf。 (我不能插入图片,因为我还是一个新手)。

(我会添加阅读内容后,我可以得到fileRoot指向正确)你添加了这个文件,但它真的被复制到你的应用程序包中吗?

解决方案

http://f.cl .ly /项目/ 1d3y2d3b101C0g07462N /屏幕%20Shot%202012-10-21%20AT%203.08.44%,20 PM.png



请确保您的RTF文件被复制到你的资源中。



其次,你只得到rtf的路径,而不是rtf的内容。

 的NSString * fileRoot = [[一个NSBundle mainBundle] pathForResource:文件路径ofType:@ RTF]; 
NSString * contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];

这将得到rtf的内容。

Why can't I read the content of foo.rtf? I've already put it in XCode bundle. fileRoot still contains null.

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

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

Appreciate anyone's help on this.


@TheAmateurProgrammer, Thank you for the diagram. I just added foo.rtf into the bundle resources. The result for fileRoot is still null. What step am I still missing?

Target's "Copy Bundle Resources" in Build Phases now contains foo.rtf. (I can't insert picture as I'm still a newbie).

(I will add the content reading after I can get fileRoot to point correctly).

解决方案

You added the file, but is it really copied to your application bundle?

http://f.cl.ly/items/1d3y2d3b101C0g07462N/Screen%20Shot%202012-10-21%20at%203.08.44%20PM.png

Make sure your rtf file is copied into your resource.

Secondly, you're only getting the path of the rtf, and not the contents of the rtf.

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

That will get the contents of the rtf.

这篇关于从XCode包中读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆