tomsmeding
tomsmeding

Reputation: 926

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?

Upvotes: 5

Views: 3228

Answers (2)

j.s.com
j.s.com

Reputation: 1478

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

let pathtoapplication: String = NSBundle.mainBundle().bundlePath

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122458

That is the bundle folder:

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

(reference).

Upvotes: 7

Related Questions