Last active
May 13, 2019 17:25
-
-
Save akashkahalkar/5f2456bad0909971c47952733f7bc37c to your computer and use it in GitHub Desktop.
File Manager Useful Functions
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
/* | |
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