How can I expand a path String with a tilde in Swift? I have a string like \"~/Desktop\"
and I\'d like to use this path with the NSFileManager
methods,
Return string:
func expandingTildeInPath(_ path: String) -> String {
return path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path)
}
Return URL:
func expandingTildeInPath(_ path: String) -> URL {
return URL(fileURLWithPath: path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path))
}
If OS less than 10.12, replace
FileManager.default.homeDirectoryForCurrentUser
with
URL(fileURLWithPath: NSHomeDirectory()