Cocoa:将参数传递给NSApplicationDelegate

22 macos xcode cocoa objective-c command-line-arguments

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

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc,  (const char **) argv);
}
Run Code Online (Sandbox Code Playgroud)

AppDelegate假的:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // how to get argc and argv?
}
Run Code Online (Sandbox Code Playgroud)

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

小智 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
}
Run Code Online (Sandbox Code Playgroud)