5

I'm trying to create a folder in Application Support Directory.

let path = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
let fileurl =  path.appendingPathComponent("my folder")
do {
try FileManager.default.createDirectory(atPath:String(describing: fileurl), withIntermediateDirectories: true, attributes: nil)
} catch {
 print(error)
}

But no folder is created.What I'm I doing wrong?

techno
  • 6,100
  • 16
  • 86
  • 192
  • 3
    My comment https://stackoverflow.com/questions/46695044/decrypted-string-always-returning-null#comment80338247_46695044 to your previous question applies here as well: Never use `String(describing:)` ! – Martin R Oct 12 '17 at 13:09

1 Answers1

7

The error occurs because you are passing the string representation of an URL which includes the file:// scheme but createDirectory(atPath expects a path without the file:// scheme.

The solution is so easy: (Always) use the URL related API of FileManager

try FileManager.default.createDirectory(at: fileurl, withIntermediateDirectories: true)

Please consider a more meaningful variable naming

  • path is actually url
  • fileurl is actually folderurl
vadian
  • 274,689
  • 30
  • 353
  • 361