Created
March 5, 2018 21:33
-
-
Save ryanmeasel/87072318950b4d44346d2c12c21a9c1a to your computer and use it in GitHub Desktop.
URL extension to create a directory in the iOS documents directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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