Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program directory in Objective-C (OSX)

I'm developing an OSX-application and in it, I'd like to know what the current directory is (i.e. the directory which holds .app-file).

At the moment, I'm using the following code:

NSString *dir=[[NSFileManager defaultManager] currentDirectoryPath];
[[NSAlert alertWithMessageText:@"dir"
                 defaultButton:@"OK"
               alternateButton:nil
                   otherButton:nil
     informativeTextWithFormat:dir] runModal];

When running from Xcode (Run-button), this gives me the debug directory (which is what I'm looking for), but when double-clicking the app in Finder (so, in the debug directory), it's giving me / which puzzles me.

Why does this happen and how can I get the current directory reliably?

like image 847
tomsmeding Avatar asked Feb 26 '13 15:02

tomsmeding


2 Answers

That is the bundle folder:

NSString *appPath = [[NSBundle mainBundle] bundlePath];

(reference).

like image 85
trojanfoe Avatar answered Oct 19 '22 06:10

trojanfoe


When programming in Apple Swift you will get the application path with:

let pathtoapplication: String = NSBundle.mainBundle().bundlePath
like image 44
j.s.com Avatar answered Oct 19 '22 06:10

j.s.com