Cocoa:将参数传递给NSApplicationDelegate

时间:2011-04-11 05:05:58

标签: objective-c xcode macos cocoa command-line-arguments

我创建了简单的Cocoa应用程序(Mac OS X 10.6),并且出现了切入点:

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc,  (const char **) argv);
}

AppDelegate虚拟:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // how to get argc and argv?
}

和其他一些。我怎样才能将argc和argv传递给AppDelegate正确的方式?

1 个答案:

答案 0 :(得分:31)

使用+[NSProcessInfo processInfo]-[NSProcessInfo arguments]

在您的申请代表中

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSArray *args = [[NSProcessInfo processInfo] arguments];
    // use -objectAtIndex: to obtain an element of the array
    // and -count to obtain the number of elements in the array
}