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,
"~/Desktop".stringByExpandingTildeInPath
NSString(string: "~/Desktop").stringByExpandingTildeInPath
NSString(string: "~/Desktop").expandingTildeInPath
Additionally you can get the home directory like this (returns a String
/String?
):
NSHomeDirectory()
NSHomeDirectoryForUser("")
In Swift 3 and OS X 10.12 it's also possible to use this (returns a URL
/URL?
):
FileManager.default().homeDirectoryForCurrentUser
FileManager.default().homeDirectory(forUser: "")
Edit: In Swift 3.1 this got changed to FileManager.default.homeDirectoryForCurrentUser