读取文档文件夹中的文本文件 - Iphone SDK [英] Read Text File in Document Folder - Iphone SDK
本文介绍了读取文档文件夹中的文本文件 - 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
显示文件中的文本。实际的文件名存储在NSUserDefault
中,名为recentDownload
。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
.语法有错误或只是简单的错误? b
Is there anything wrong with the syntax or just simple error?
推荐答案
NSBundle
类用于在应用程序包中查找内容,捆绑,所以你生成路径的方式将无法工作。尝试这样: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:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];
这篇关于读取文档文件夹中的文本文件 - Iphone SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文