简体   繁体   English

如何删除应用程序沙箱中的文件?

[英]How to delete files in the app's sandbox?

I have a UITableView set up and its data source is the app's sandbox, so all the rows are filled with the files that were imported using UIDocumentPicker , but I also need to be able to delete those files. 我有一个UITableView设置,其数据源是应用程序的沙箱,因此所有行都填充了使用UIDocumentPicker导入的文件,但我还需要能够删除这些文件。

The delete function works, I am able to slide and delete a row and everything, but the file stays in the app's sandbox, so every time I import a new file, the rows are refilled (the TableView reloads every time something is imported) with the previously "deleted" stuff. 删除功能有效,我能够滑动并删除行和所有内容,但文件仍保留在应用程序的沙箱中,因此每次导入新文件时,行都会重新填充(每次导入时都会重新加载TableView )以前“删除”的东西。

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCell.EditingStyle.delete {
            self.deleteFile()
            importedfiles.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
        }

 }

Plus everything that is in the code already, I need the function to delete the file from the app's sandbox (importedfiles). 加上代码中的所有内容,我需要从应用程序的沙箱(importedfiles)中删除文件的功能。

Here's what I've got so far, I am able to delete files, but only the entire directory, which is not what I want. 这是我到目前为止所做的,我能够删除文件,但只能删除整个目录,这不是我想要的。 Code: 码:

   func deleteFile() {
        let dirPaths = FM.urls(for: .documentDirectory, in: .userDomainMask)
        let docsDir = dirPaths[0].path

        if FM.fileExists(atPath: docsDir) {
            do {
                try FM.removeItem(atPath: docsDir)
            } catch {
                print("Could not delete file: \(error)")
            }
        }
    }

Edit: "importedfiles" are files that were imported in the app's directory (documents) using UIDocumentPickerViewController. 编辑:“importedfiles”是使用UIDocumentPickerViewController在应用程序目录(文档)中导入的文件。 And the TableView uses this data to create cells. TableView使用此数据创建单元格。

Change your delete method to 将删除方法更改为

func deleteFile(_ url: URL) {
    let fm = FileManager.default
    do {
        try fm.removeItem(at: url)
    } catch {
        print(error)
    }
}

and call it 并称之为

if editingStyle == UITableViewCell.EditingStyle.delete {
    self.deleteFile(importedfiles[indexPath.row])
    importedfiles.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}

This should work but there is no error handling and if you want that you can define your method to return Result 这应该有效但没有错误处理,如果你想要,你可以定义你的方法来返回Result

func deleteFile(_ url: URL) -> Result<Void,Error> {
    let fm = FileManager.default
    do {
        try fm.removeItem(at: url)
        return .success(())
    } catch {
        return .failure(error)
    }
}

and handle the result in a switch 并在switch处理结果

let result = deleteFile(importedfiles[indexPath.row])
switch result {
case .success:
    //Maybe update array and table view here
case .failure(let error):
     //dislplay error?
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM