读取文档文件夹中的文本文件 - Iphone SDK
我的代码如下:
NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentDownload"];
NSString *fullPath = [NSBundle pathForResource:fileName ofType:@"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:@"/Documents/"]];
NSError *error = nil;
[textViewerDownload setText:[NSString stringWithContentsOfFile:fullPath encoding: NSUTF8StringEncoding error:&error]];
textviewerdownload
是显示文件中文本的textview
。实际文件名存储在名为recentDownload
的NSUserDefault
中。当我构建这个时,我单击了它下面的
按钮
,我的应用程序崩溃了
。语法有什么问题还是只是简单的错误?
I have this code below:
NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentDownload"];
NSString *fullPath = [NSBundle pathForResource:fileName ofType:@"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:@"/Documents/"]];
NSError *error = nil;
[textViewerDownload setText:[NSString stringWithContentsOfFile:fullPath encoding: NSUTF8StringEncoding error:&error]];
textviewerdownload
is thetextview
displaying the text from the file. The actual file name is stored in anNSUserDefault
calledrecentDownload
.When I build this, I click the
button
which this is under, and my applicationcrashes
.Is there anything wrong with the syntax or just simple error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSBundle 类用于在应用程序包中查找内容,但 Documents 目录位于包之外,因此生成路径的方式将不起作用。试试这个:
The
NSBundle
class is used for finding things within your applications bundle, but the Documents directory is outside the bundle, so the way you're generating the path won't work. Try this instead:无论如何,这对我有用
,谢谢大家..
This worked for me
Anyway, thank you all..
对于从文本文件读取/写入,请检查此 url< /a>.
For read/write from text file check this url.