简体   繁体   English

尝试从文件目录中删除文件

[英]Trying to delete files from documents directory

I have an app that allows users to record multiple videos and then post them to view later from a database. 我有一个应用程序,允许用户录制多个视频,然后发布它们以供以后从数据库中查看。 After they post the video, I want the app to delete the video from the documents directory to save phone storage. 他们发布视频后,我希望该应用程序从文档目录中删除视频,以节省手机存储空间。 I am trying this, but when i check my phone's storage nothing is updated. 我正在尝试此操作,但是当我检查手机的存储空间时,没有任何更新。 Here is what I am doing 这是我在做什么

This is where i write the video to: 这是我将视频编写到的位置:

func videoFileLocation() -> String {
    let uniqueID = NSUUID().uuidString
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
    let outputPath = "\(documentsPath)/\(uniqueID).mov"
    return outputPath
}

This is how I remove them: 这是我删除它们的方法:

func clearDirectory() {
    do {
        data = try context.fetch(VideoPath.fetchRequest())

        for each in data {
            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let path = "\(documentsPath)/\(each.fileLocations!)"
            let url = URL(fileURLWithPath: path)
            do {
            try FileManager.default.removeItem(at: url)
                print("Successfully Cleared")
            } catch {
                print("There was a problem removing the file")
            }
}

The successfully cleared method gets printed out, but I see no change in my phone's storage. 成功清除的方法会被打印出来,但是我的手机存储空间没有变化。 What am i doing wrong? 我究竟做错了什么?

Replace 更换

let path = "\(documentsPath)/\(each.fileLocations!)"

with below and try. 与下面并尝试。

var path: String = nil

if let fileLocationPath = each.fileLocations as? String {
  path = documentsPath.appendingPathComponent(fileLocationPath)
}

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

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