ios - 从 Swift 4 的文件目录中删除用户选择的文件?

标签 ios swift file

您好,我有以下代码作为函数从文件目录中删除文件。但是当传递单个文件名字符串时它似乎工作了几次但是当我将它用作函数并传递文件名时,它不起作用。 代码如下。 我在 Swift 4.1 中使用 Xcode 9.2

//USAGE
//User Clicks on a delete button and that will present an Alert Dialog with a list of files and user clicks on the file he/she would like to delete
/////////////////////////////////////////////////////
@IBAction func deleteConfigAction(_ sender: Any) {
    //---
    //---

    //Another function is called to list all files in the files directory and then store it into a string variable
    var file1: String = "Empty"

    //Calling another function to load all files present
    var userFilesPresent : [String] = listFilesPresent()!

    //For Example
    file1 = userFilesPresent[0]

    //---
    let alert = UIAlertController(title: "Delete File", message: "Select a file to be deleted", preferredStyle: UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: file1, style: .default,handler: { (action: UIAlertAction!) in

        self.deleteFileSelected(fileName: file1)

        print("file deleted =",file1)

    }))

    alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.cancel, handler: nil))

    // show the alert
    self.present(alert, animated: true, completion: nil)
}

//----------------------------------------------------------------
//FUNCTION

func deleteFileSelected(fileName:String) -> [String]? {
    //Set Files directory path

    let mypath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)

    if mypath.count > 0 {

        let directoryPath = mypath[0]

        //Filter for txt files
        let searchfilePath = NSString(format:"%@/%@.txt", directoryPath, fileName) as String

        if FileManager.default.fileExists(atPath: searchfilePath) {
            do {
                //file exists, now try deleting the file
                try FileManager.default.removeItem(atPath: searchfilePath)
                print("File deleted")
            } catch {
                print("Error")
            }
        }
    }

    return nil
}

最佳答案

您需要通过将文件名附加到目录的基本 url 来创建文件路径

func delete(fileName : String)->Bool{
    let fileManager = FileManager.default
    let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let filePath = docDir.appendingPathComponent(fileName)
    do {
        try FileManager.default.removeItem(at: filePath)
        print("File deleted")
        return true
    }
    catch {
        print("Error")
    }
    return false
}

关于ios - 从 Swift 4 的文件目录中删除用户选择的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344169/

相关文章:

swift - 高效获取评论集合查询的用户信息

ios - UITableViewCell 删除滑动动画丑陋

javascript - 如何将 <html contenteditable> 内容保存为文本文件

python - 我如何用 Python 发送这封电子邮件,打开文件等?

c# - 从 iCloud 打开自定义文件到 Xamarin.iOS 应用程序不起作用

ios - 如何使用 pjsip 2.5.5 或 2.6 在 Ios 中进行视频通话?

iphone - 更新 UIButton 背景时的延迟

iOS 自动补全不适用于 RxCocoa Xcode 8.3

arrays - 如何在 swift 3 中将带有自定义类的数组转换为 JSON

windows - 如何找到 Windows 可执行文件在运行时打开或访问的所有文件?