DevZer0 DevZer0 - 6 months ago 20
Swift Question

Get the absolute path of .app bundle in OSX using swift

I am trying to find the path of where a simple OS-X app is running from. i tried this code

let fileManager = NSFileManager.defaultManager()
let path = fileManager.currentDirectoryPath


but
path
always has value
/
how do i get the absolute path, such as if the
.app
is executed from
/Users/xxx/sandbox/sample.app
i would like to get the value
/Users/xxx/sandbox


is it possible?

Answer

Use NSBundle:

let path = NSBundle.mainBundle().bundleURL.URLByDeletingLastPathComponent!.path!