Reputation: 159
I try to save data into document directory. I do not get any error but it never saves the data. Always it says "File does not exist, create it".
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)
}
}
}
Upvotes: 0
Views: 251
Reputation: 285220
Replace
if fileManager.fileExists(atPath: fileURL.absoluteString) {
with
if fileManager.fileExists(atPath: fileURL.path) {
Never use absoluteString
in a file system URL
Upvotes: 4