读取文档文件夹中的文本文件 - Iphone SDK [英] Read Text File in Document Folder - Iphone SDK

查看:223
本文介绍了读取文档文件夹中的文本文件 - 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 the textview displaying the text from the file. The actual file name is stored in an NSUserDefault called recentDownload.

      当我建立这个,我点击按钮这是下,我的应用程序崩溃

      When I build this, I click the button which this is under, and my application crashes.

      语法有错误或只是简单的错误? 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屋!

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