“@UIApplicationMain"有什么作用?意思? [英] What does "@UIApplicationMain" mean?
问题描述
我刚刚创建了我的第一个 Swift 项目,在 AppDelegate.swift
中的类声明上方有一行 - 为什么会在那里?!
I just created my first Swift project, in the AppDelegate.swift
there is a line above a class declaration - why is it there?!
...
import UIKit
import CoreData
@UIApplicationMain // <- WHY IS IT HERE?
class AppDelegate: UIResponder, UIApplicationDelegate {
...
推荐答案
Swift 中的 @UIApplicationMain
属性替换了 Objective-C 项目中的琐碎的 main.m
文件(其目的是实现作为所有 C 程序入口点的 main
函数并调用 UIApplicationMain
来启动 Cocoa Touch 运行循环和应用程序基础结构).
The @UIApplicationMain
attribute in Swift replaces the trivial main.m
file found in Objective-C projects (whose purpose is to implement the main
function that's the entry point for all C programs and call UIApplicationMain
to kick off the Cocoa Touch run loop and app infrastructure).
在 Objective-C 中,UIApplicationMain
函数提供的每个应用程序配置的主要 (heh) 位是将应用程序的自定义类之一指定为共享 UIApplication代码> 对象.在 Swift 中,您可以通过添加
@UIApplicationMain
属性 到该类的声明.(如果有理由,您仍然可以直接调用 UIApplicationMain
函数.在 Swift 中,您将该调用放在 main.swift
文件中的顶级代码中.)
In Objective-C, the main (heh) bit of per-app configuration that the UIApplicationMain
function provides is designating one of your app's custom classes as the delegate of the shared UIApplication
object. In Swift, you can easily designate this class by adding the @UIApplicationMain
attribute to that class' declaration. (You can also still invoke the UIApplicationMain
function directly if you have reason to. In Swift you put that call in top-level code in a main.swift
file.)
@UIApplicationMain
仅适用于 iOS.在 OS X 中,应用程序委托传统上设置在 Info.plist 指定的主 nib 文件中(Swift 和 ObjC 相同)——但在 OS X 故事板中没有主 nib 文件,所以 @NSApplicationMain
在那里做同样的事情.
@UIApplicationMain
is for iOS only. In OS X, the app delegate is traditionally set in the main nib file designated by the Info.plist (the same for Swift as for ObjC) — but with OS X storyboards there's no main nib file, so @NSApplicationMain
does the same thing there.
这篇关于“@UIApplicationMain"有什么作用?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!