@NSApplicationMain在XCode中的Swift编程项目中有什么用 [英] What is use of @NSApplicationMain in swift programming project in xcode

查看:123
本文介绍了@NSApplicationMain在XCode中的Swift编程项目中有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Xcode created AppDelegate.swift with following code

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
    }


}

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

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

推荐答案

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

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."

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

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.swift file for a new OS X project that looked like this:

import Cocoa

NSApplicationMain(Process.argc, Process.unsafeArgv)

那是主要的切入点.现在,Xcode不再费心生成此文件.相反,它只是使用@NSApplicationMain标记您的应用程序委托,并且此引导步骤是在后台处理的.

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

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

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

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

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

If you do not use this attribute, supply a main.swift file with a main function that calls the NSApplicationMain function. For example, if your app uses a custom subclass of NSApplication as its principal class, call the NSApplicationMain function instead of using this attribute.

这篇关于@NSApplicationMain在XCode中的Swift编程项目中有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆