我想将文件从文档/收件箱移动到内部应用程序。我已经在下面写了。但是有些不对劲。
class FileListViewController: UITableViewController {
class func move(_ atPathName: String, name: String, toPathName: String) -> Bool {
let fileManager = FileManager.default
do {
try fileManager.moveItem(atPath: atPathName, toPath: toPathName)
} catch {
return false
}
return true
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedVRMpath = NSHomeDirectory() + "/Documents/Inbox/(indexPath.row)"
let newVRMpath:String? = Bundle.main.path( forResource: "Febboy2", ofType: "vrm" )
let selectedNameinList = newarray[indexPath.row]
FileListViewController.move(selectedVRMpath, name: selectedNameinList, toPathName: newVRMpath)
错误信息:
可选类型"字符串?"的值必须解包为类型为"字符串"的值
.vrm 文件约为 10MB
最后,我在 ViewController 中使用了一个文件。我需要将文件从文档/收件箱移动到项目内部。如果我误解了这个解决方案,请给我一个建议。我无法直接使用VRM文件在其他类中选择URL文档/收件箱。
---
感谢您的回复提示,我像这样更改了代码。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedVRMpath = NSHomeDirectory() + "/Documents/Inbox/(indexPath.row)"
guard let newVRMpath:String = Bundle.main.path( forResource: "Febboy2", ofType: "vrm" ) else { return }
let selectedNameinList = newarray[indexPath.row]
print("Selected! (self.newarray[indexPath.row])")
FileListViewController.move(selectedVRMpath, name: selectedNameinList, toPathName: newVRMpath)
}
错误消息消失。但我知道还有问题。
您需要
强制解开包装newVRMpath
或使用guard
guard let newVRMpath = Bundle.main.path( forResource: "Febboy2", ofType: "vrm" ) else { return }
FileListViewController.move(selectedVRMpath, name: selectedNameinList, toPathName: newVRMpath!)
这将修复编译错误,但无法正常工作,因为您无法写入主捆绑包,您需要从文档文件夹创建新路径