Cocoa: Pass arguments to NSApplicationDelegate

房东的猫 提交于 2019-12-20 09:17:05

问题


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:


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
}


来源:https://stackoverflow.com/questions/5616959/cocoa-pass-arguments-to-nsapplicationdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!