'"The folder 'Documents' doesn't exist" error in Swift [duplicate]
I am trying to get a list of files in document folder in Swift, here's a code snippet:
let fileManager = FileManager.default
if let dir = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
print(dir.absoluteString) // print 1
do {
let files = try fileManager.contentsOfDirectory(atPath: dir.absoluteString)
print(files) // print 2
} catch {
print(error.localizedDescription) // print 3
}
}
Then "print 1" prints:
file:///Users/.../.../Documents/
And "print 3" prints:
The folder "Documents" doesn't exist.
Currently, there are quite a few files saved in that folder, at very least, my default.realm file is there. Why does this happen?
Solution 1:[1]
You should use dir.path in order to convert the URL to a file path:
let files = try fileManager.contentsOfDirectory(atPath: dir.path)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rmaddy |
