Search code examples
swiftmacosnsurl

How to get user path with NSURL


I'm trying to find the Swift equivalent of ~/.

So, I want to create an NSURL with the user path. I've looked at NSURLComponents, but I can't see anything there. The user var seems only to apply to existing URLs. I'm sure this is pretty basic, but haven't found anything.


Solution

  • To get the home directory in Swift you just need to use the following:

    let homeDirectory = NSHomeDirectory()
    

    This will print out:

    /Users/FOLDER_NAMED_AFTER_USER