2013-02-26 29 views
5

我正在開發一個OSX應用程序,並且在其中,我想知道當前目錄是什麼(即保存.app文件的目錄)。Objective-C中的程序目錄(OSX)

目前,我使用下面的代碼:

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

當從Xcode中(運行按鈕)運行,這給了我調試目錄(我要找的),但是當雙擊Finder中的應用程序(所以,在調試目錄中),它給我/這讓我感到困惑。

爲什麼會發生這種情況,如何可靠地獲取當前目錄?

回答

7

即束文件夾:

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

reference)。

+0

謝謝,這是我一直在尋找! (實際上,不是_exactly_:我使用'[[[NSBundle mainbundle] bundlePath] stringByDeletingLastPathComponent]'得到我想要的:)) – tomsmeding 2013-02-26 15:23:51

+1

@tomsmeding啊是的,我錯過了*持有位* -文件」。 – trojanfoe 2013-02-26 15:27:57

2

當蘋果斯威夫特編程您將獲得應用程序的路徑有:

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