iPad 文本文件导入
我正在尝试使用 initWithContentsOfFile:encoding:error: 方法将文本文件导入到我的 iPad 应用程序中,但是当我尝试获取文本文件的文件路径时,我遇到了一些麻烦。看来我的文本文件没有被发现。下面的调用返回nil(文本文件名为legal.txt)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"legal"
ofType:@"txt"];
显然,在这个返回nil之后,我的调用...
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:@"error"];
也返回nil。
我对此很陌生,所以我可能做了一些明显不正确的事情,但看了遍SO和其他地方,找不到任何人和我有同样问题的人。
I'm trying to import a text file into my iPad application using the method initWithContentsOfFile:encoding:error:, but when I try to get the filepath to my text file, I am running into some troubles. It seems that my text file is not being found. The following call returns nil (the text file is called legal.txt)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"legal"
ofType:@"txt"];
Obviously, after this returns nil, my call...
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:@"error"];
also returns nil.
I am quite new at this so I might be doing something obviously incorrect, but looked all over SO and elsewhere and couldn't find anyone with the same problem as me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用 pathForResource:ofType:inDirectory:
也通过传递目录
Call pathForResource:ofType:inDirectory:
by passing the directory as well
查看应用目标的复制资源构建阶段,并确保
legal.txt
已将其添加到您的应用捆绑包中。您还可以在构建文件夹中查看已编译的应用程序包(Xcode 4 中默认为~/Library/Developer/Xcode/DerivedData/AppName-.../
)。Look in your app target's Copy Resources build phase and make sure
legal.txt
is making it into your app bundle. You can also look inside the compiled app package in your build folder (~/Library/Developer/Xcode/DerivedData/AppName-.../
by default in Xcode 4).