我尝试将数据保存到文档目录中。我没有收到任何错误,但它从不保存数据。它总是说“文件不存在,创建它”。
let fileManager = FileManager.default
if let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).last {
let fileURL = documentsDirectory.appendingPathComponent("example")
if fileManager.fileExists(atPath: fileURL.absoluteString) {
print("File exists")
} else {
print("File does not exist, create it")
do {
try myData.write(to: fileURL)
print("data saved")
} catch {
print(error)
}
}
}
Replace
if fileManager.fileExists(atPath: fileURL.absoluteString) {
with
if fileManager.fileExists(atPath: fileURL.path) {
切勿使用absoluteString
在文件系统 URL 中
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)