-
-
Save asaday/4c2c5490bf21fc8e769174d6b8fddf08 to your computer and use it in GitHub Desktop.
This file contains 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
public struct Path { | |
public static var documents: String { return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] } | |
public static var caches: String { return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] } | |
public static var library: String { return NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0] } | |
public static var support: String { return NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)[0] } | |
public static var temp: String { return NSTemporaryDirectory() } | |
public static var resource: String { return Bundle.main.resourcePath ?? "" } | |
@discardableResult public static func remove(_ path: String) -> Bool { | |
do { | |
try FileManager.default.removeItem(atPath: path) | |
} catch { return false } | |
return true | |
} | |
@discardableResult public static func mkdir(_ path: String) -> Bool { | |
do { | |
try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil) | |
} catch { return false } | |
return true | |
} | |
public static func files(_ path: String) -> [String] { | |
return (try? FileManager.default.contentsOfDirectory(atPath: path)) ?? [] | |
} | |
public static func exists(_ path: String) -> Bool { | |
return FileManager.default.fileExists(atPath: path) | |
} | |
public static func isFile(_ path: String) -> Bool { | |
var isdir: ObjCBool = false | |
let exist = FileManager.default.fileExists(atPath: path, isDirectory: &isdir) | |
return exist && !isdir.boolValue | |
} | |
public static func isDir(_ path: String) -> Bool { | |
var isdir: ObjCBool = false | |
let exist = FileManager.default.fileExists(atPath: path, isDirectory: &isdir) | |
return exist && isdir.boolValue | |
} | |
public static func attributes(_ path: String) -> [FileAttributeKey : Any] { | |
return (try? FileManager.default.attributesOfItem(atPath: path)) ?? [:] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment