Skip to content

Instantly share code, notes, and snippets.

@akashkahalkar
Last active May 13, 2019 17:25
Show Gist options
  • Save akashkahalkar/5f2456bad0909971c47952733f7bc37c to your computer and use it in GitHub Desktop.
Save akashkahalkar/5f2456bad0909971c47952733f7bc37c to your computer and use it in GitHub Desktop.
File Manager Useful Functions
/*
isDirectory is Objective-C pointer
kind of black magic that u can use to check is directory exists or not
*/
func directoryExistsAtPath(_ path: String) -> Bool {
var isDirectory = ObjCBool(true)
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}
func getDocumentDirectoyPath() -> URL? {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
}
func getAllFiles(at path: URL) {
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment