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()在哪里?
解决方法
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 amain
function that calls theNSApplicationMain
function. For example,if your app uses a custom subclass ofNSApplication
as its principal class,call theNSApplicationMain
function instead of using this attribute.