iOS - FileManager 不会删除文档目录中的 json 文件

标签 ios json swift xcode

我在文档目录中创建了一个文件,出于某种原因,当我尝试使用下面的代码删除它时,它没有被删除。代码没有抛出任何错误,但文件仍然存在

let fileManager = FileManager.default
if let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
    let filePath = documentDirectory.appendingPathComponent("data.json")
    do {
        try fileManager.removeItem(atPath: filePath.path)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

我也试过把它放在另一个文件夹里,然后删除文件夹,但仍然是同样的问题。

这是存储文件的路径:

Users/user1/Library/Developer/CoreSimulator/Devices/76AFDB69-75C8-464E-93F2-6ABF622068FD/data/Containers/Data/Application/7D268156-977A-4A3C-834B-6B13FA3DE76D/Documents/

最佳答案

你可以像下面这样尝试

if let filePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("data.json"), fileManager.fileExists(atPath: filePath.path) {
    do {
        try fileManager.removeItem(atPath: filePath.path)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

关于iOS - FileManager 不会删除文档目录中的 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51363168/

相关文章:

swift - 如何知道swift中数字的单位?

IOS Google OAuth 1 次认证成功后出现错误

javascript - 如何对包含 HTML 字符的 JSON 字符串进行编码?

ios - 使用 Parse 和 Swift 对一个键的两个查询约束

ios - 我怎么知道 AVAudioPlayer 是否暂停了?

json - 在 .net Standard 应用程序上本地保存数据

iphone - 当我们改变设备方向时查看设置默认大小

ios - 如果应用程序处于终止/强制关闭(不在后台)状态,收到推送通知时将调用什么委托(delegate)方法?

ios - 如果陈述有脾气

json - 折叠 JSON 字符串的正确语法是什么?