xcode swift 编程项目中@NSApplicationMain 的用途是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27469436/
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-15 06:21:33  来源:igfitidea点击:

What is use of @NSApplicationMain in swift programming project in xcode

xcodemacosswift

提问by Omkar

I've created a sample project using Xcode 6 and language as swift.

我已经使用 Xcode 6 和语言快速创建了一个示例项目。

Xcode created AppDelegate.swift with following code

Xcode 使用以下代码创建了 AppDelegate.swift

import Cocoa

@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


}

What does @NSApplicationMain mean and where is my main.mm/main.m file with main()?

@NSApplicationMain 是什么意思,我的带有 main() 的 main.mm/main.m 文件在哪里?

回答by Matt Gibson

As it mentioned in the release notes:

正如发行说明中提到的:

OS X apps can now apply the @NSApplicationMain attribute to their app delegate class in order to generate an implicit main for the app. This attribute works in the same way as the @UIApplicationMain attribute for iOS apps."

OS X 应用程序现在可以将 @NSApplicationMain 属性应用于其应用程序委托类,以便为应用程序生成隐式 main。此属性的工作方式与 iOS 应用程序的 @UIApplicationMain 属性相同。”

So, basically, it's a way of getting your app bootstrapped and running without having to write some global-level boilerplate in a separate Swift file. In previous versions of Xcode, when you created a new Swift Cocoa project, Xcode would generate a main.swiftfile for a new OS X project that looked like this:

所以,基本上,这是一种让您的应用程序启动和运行的方法,而无需在单独的 Swift 文件中编写一些全局级别的样板。在以前的 Xcode 版本中,当您创建新的 Swift Cocoa 项目时,Xcode 会main.swift为新的 OS X 项目生成一个文件,如下所示:

import Cocoa

NSApplicationMain(Process.argc, Process.unsafeArgv)

And that was the main entry point. Now Xcode doesn't bother generating this file; instead it just tags your app delegate with @NSApplicationMainand this bootstrapping step is handled behind the scenes.

这是主要的切入点。现在 Xcode 不用费心生成这个文件了;相反,它只是标记您的应用程序委托,@NSApplicationMain并且此引导步骤在幕后处理。

More info from The Swift Programming Language:

来自Swift 编程语言的更多信息:

Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the NSApplicationMainfunction and passing this class's name as the name of the delegate class.

If you do not use this attribute, supply a main.swiftfile with a mainfunction that calls the NSApplicationMainfunction. For example, if your app uses a custom subclass of NSApplicationas its principal class, call the NSApplicationMainfunction instead of using this attribute.

将此属性应用于类以指示它是应用程序委托。使用此属性等效于调用NSApplicationMain函数并将此类的名称作为委托类的名称传递。

如果不使用此属性,请提供main.swift带有main调用该NSApplicationMain函数的函数的文件。例如,如果您的应用程序使用 的自定义子类NSApplication作为其主要类,请调用该NSApplicationMain函数而不是使用此属性。