Cocoa: Pass arguments to NSApplicationDelegate

2019-03-09 09:42发布

I have created the simple Cocoa application (Mac OS X 10.6) and there have appeared the entry point:

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

and AppDelegate dummy:

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

and some other. How could I pass the argc and argv to my AppDelegate right way?

1条回答
We Are One
2楼-- · 2019-03-09 10:24

Use +[NSProcessInfo processInfo] and -[NSProcessInfo arguments].

In your application delegate,

- (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
}
查看更多
登录 后发表回答