FileManager.default.removeItem不删除文件 [英] FileManager.default.removeItem not removing file

查看:883
本文介绍了FileManager.default.removeItem不删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FileManager.default.removeItem 从文档目录中删除文件,但不是在模拟器上删除文件。这是我的代码:

I'm trying to remove a file from the documents directory using FileManager.default.removeItem but is not deleting the file on the simulator. Here is my code:

if let dir = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
            do{
                let path = dir.appendingPathComponent(file).absoluteString
                do{
                    try FileManager.default.removeItem(atPath:path)
                }catch{
                    print(error)
                }
            }
        }

但我总是失败。你们中的任何人都知道它失败的原因吗?

But I always fails. Any of you knows why it fails?

推荐答案

你可以像这样写:

var filemanager = FileManager.default
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as NSString
let destinationPath = documentsPath.appendingPathComponent("Filename.jpg")
try! filemanager.removeItem(atPath: destinationPath)

这篇关于FileManager.default.removeItem不删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆