我有一个应用程序,允许用户录制多个视频,然后发布它们以便以后从数据库中查看。发布视频后,我希望应用程序从文档目录中删除视频以节省手机存储空间。我正在尝试这个,但当我检查我的手机存储时,没有任何更新。这就是我正在做的事情
这是我将视频写入:
的地方func videoFileLocation() -> String {
let uniqueID = NSUUID().uuidString
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let outputPath = "\(documentsPath)/\(uniqueID).mov"
return outputPath
}
这就是我删除它们的方法:
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")
}
}
成功清除的方法被打印出来,但我看到我的手机存储空间没有变化。我做错了什么?
答案 0 :(得分:1)
替换
let path = "\(documentsPath)/\(each.fileLocations!)"
以下并尝试。
var path: String = nil
if let fileLocationPath = each.fileLocations as? String {
path = documentsPath.appendingPathComponent(fileLocationPath)
}