我有以下代码:
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
中。当我构建它时,我点击了它所在的
按钮
,然后我的应用程序崩溃了
。语法有问题还是只是简单的错误?
最佳答案
NSBundle
类用于在您的应用程序包中查找内容,但 Documents 目录在包之外,因此您生成路径的方式将不起作用。试试这个:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];
关于ios - 读取文档文件夹中的文本文件 - Iphone SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709705/