1

I have found a problem when I want to delete a folder and its containers.

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let downloadDirectory:URL = urls.appendingPathComponent("Download")
let manager = FileManager.default

let pathString = downloadDirectory.absoluteString // file:///Users/...
let path = downloadDirectory.path // /Users/...

then

let  directoryContents:[Any]? = try? manager.contentsOfDirectory(atPath: path)

works well, but

let  directoryContents:[Any]? = try? manager.contentsOfDirectory(atPath: pathString)

can't find the folder

Sheryl
  • 85
  • 5
  • Make sure to always use the path property when using a method that has a path parameter. Note that if the method also has the option to pass an URL instead of a path it is preferred to use the URL. https://developer.apple.com/documentation/foundation/filemanager/1413768-contentsofdirectory – Leo Dabus Feb 24 '18 at 06:55
  • It is also worth mentioning when going the opposite way (initialize an URL from a string) if your string contains the url scheme (file://) you need to use `URL(string:)` initializer. If it doesn't contain the scheme you need to use the file url initializer `URL(fileURLWithPath:)` – Leo Dabus Feb 24 '18 at 07:02
  • 1
    @Leo Dabu That's very helpful! Thanks a lot! – Sheryl Feb 26 '18 at 08:22

0 Answers0