Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The folder 'Documents' doesn't exist" error in Swift [duplicate]

Tags:

ios

swift

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?

like image 912
K.Wu Avatar asked Aug 31 '26 10:08

K.Wu


1 Answers

You should use dir.path in order to convert the URL to a file path:

let files = try fileManager.contentsOfDirectory(atPath: dir.path)

like image 97
Ali Moazenzadeh Avatar answered Sep 03 '26 01:09

Ali Moazenzadeh