xcode Cocoa:将参数传递给 NSApplicationDelegate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5616959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:46:43  来源:igfitidea点击:

Cocoa: Pass arguments to NSApplicationDelegate

objective-cxcodemacoscocoacommand-line-arguments

提问by

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

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

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

and AppDelegatedummy:

AppDelegate假人:

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

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

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

回答by

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

使用+[NSProcessInfo processInfo]-[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
}