ios - 读取文档文件夹中的文本文件 - Iphone SDK

标签 ios iphone objective-c cocoa documents

我有以下代码:

    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。实际文件名存储在名为 recentDownloadNSUserDefault 中。

  • 当我构建它时,我点击了它所在的按钮,然后我的应用程序崩溃了

  • 语法有问题还是只是简单的错误?

最佳答案

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/

相关文章:

ios - 如何在 iOS 上录制来自其他应用程序的屏幕内容?是否存在关于隐私(应用程序沙箱)的安全问题?

ios - 测试在 iOS 中使用 KIF 启动的外部浏览器

ios - 返回数组并检查是否为 nil 的正确方法

iphone - 启动图像 - iPhone 应用程序

ios - 从数组 iOS 获取 EKEvent

iphone - 在 UICollectionView 中设置边框

iOS Rxswift 处理 CancelButton 在 searchBar 中点击

iphone - UTF-8 转换

ios - CollectionView 中单元格之间的间距

ios - 如何在iOS 6/7中将图像保存到相机胶卷/相册?