ios - 如何从 iOS 文档目录复制文本文件

标签 ios cocoa-touch nsfilemanager

您好,我正在尝试将文本文件从文档目录复制到位于文档目录中的文件夹名称 Temp 中。

这是我尝试过但不起作用的一些代码。

self.fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *documentsDirectoryToTemp = [NSString stringWithFormat:@"%@/Temp/",[paths objectAtIndex:0]];

        NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",tblGetNameToMove.textLabel.text]];

        [self.fileManager copyItemAtPath:txtPath toPath:documentsDirectoryToTemp error:&error];

我怎样才能做到这一点?

最佳答案

造成这种情况的原因可能有很多:

  1. 首先尝试打印所有路径,如documentsDirectoryToTempn和txtPath,无论是否正确。

  2. 第二件事是使用错误变量:

    如果(错误) { NSLog("处理时出错:%@",[error localizedDiscription]); }

    这样您就可以跟踪确切的问题。

关于ios - 如何从 iOS 文档目录复制文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826923/

相关文章:

ios - 如何防止 UITableViewCell 的 textLabel 在 iPad 上截断其文本

objective-c - 浮点工作正常......大多数时候

objective-c - 如何使用最新的 Facebook SDK 从 iOS 向 Facebook 发布内容?

iphone - iPhone MIME 类型数据库在哪里?

ios - 如何在不先将文件下载到设备的情况下从 iCloud 删除文件?

iOS 从具有开发者资料的应用商店中退出 IPA

ios - 如何使用flutter,wordpress在我的应用程序中添加拉动刷新?

ios - 删除 dealloc 上对象的所有观察信息

Swift:如何在路径字符串中扩展波浪号

objective-c - 使用 NSData 或 NSFileManager 检查文件是否存在之间的区别