Skip to content

Instantly share code, notes, and snippets.

@ryanmeasel
Created March 5, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmeasel/87072318950b4d44346d2c12c21a9c1a to your computer and use it in GitHub Desktop.
Save ryanmeasel/87072318950b4d44346d2c12c21a9c1a to your computer and use it in GitHub Desktop.
URL extension to create a directory in the iOS documents directory
extension URL {
static func createDirectory(_ dirName: String) -> URL? {
let fileManager = FileManager.default
if let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
let dirPath = documentDirectory.appendingPathComponent(dirName)
if !fileManager.fileExists(atPath: dirPath.path) {
do {
try fileManager.createDirectory(atPath: dirPath.path,
withIntermediateDirectories: true,
attributes: nil)
} catch {
print(error.localizedDescription)
return nil
}
}
return dirPath
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment