在xcode中的swift编程项目中使用了@NSApplicationMain

我使用 Xcode 6创建了一个示例项目,而swift则使用了语言.

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
    }


}

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

解决方法

正如 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.”

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

import Cocoa

NSApplicationMain(Process.argc,Process.unsafeArgv)

那是主要的入口点.现在Xcode并不打扰生成这个文件;相反,它只是使用@NSApplicationMain标记您的应用程序委托,并且这个引导步骤在幕后处理.

The Swift Programming Language更多信息:

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.

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.

以上是来客网为你收集整理的在xcode中的swift编程项目中使用了@NSApplicationMain全部内容,希望文章能够帮你解决在xcode中的swift编程项目中使用了@NSApplicationMain所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。