AppDelegate.swift 如何替换 Xcode 6.3 中的 AppDelegate.h 和 AppDelegate.m [英] How does AppDelegate.swift replace AppDelegate.h and AppDelegate.m in Xcode 6.3
问题描述
根据 iOS 开发者库><块引用>
应用委托是您编写自定义应用级代码的地方.与所有类一样,AppDelegate 类在应用的两个源代码文件中定义:接口文件 AppDelegate.h 和实现文件 AppDelegate.m.
然而,在 Xcode 6.3 中似乎只有 AppDelegate.swift 而不再有 .h 和 .m 扩展名.我想了解 .swift 如何同时替换 .h 和 .m 扩展名.
简单的答案是 AppDelegate.swift 只是 Objective-C AppDelegate.h
和 AppDelegate.m 的翻译
,因为 Swift 不需要单独的头文件和实现,而是需要一个 .swift 文件.
然而,在幕后,两者之间还有其他主要区别.
在 Objective-C 中,存在一个 main.m
文件,其唯一目的是实例化 UIApplicationMain
在 Swift 中,位于 AppDelegate.swift 顶部的 @UIApplicationMain
注释标签取代了 Objective-C 中 main.m 文件中存在的任何主函数的需要.如果省略此标记,您可以使用 main.swift 文件使用指定的 App Delegate 实例化您的 UIApplication
.
一个 main.swift 实现看起来像这样:
导入 UIKit自动释放池{UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))}
According to the iOS Developer Library
The app delegate is where you write your custom app-level code. Like all classes, the AppDelegate class is defined in two source code files in your app: in the interface file, AppDelegate.h, and in the implementation file, AppDelegate.m.
However, in Xcode 6.3 it appears that there is only AppDelegate.swift and no longer the .h and .m extensions. I would like to understand how the .swift replaced both the .h and .m extensions.
The simple answer is that AppDelegate.swift is just the translation from Objective-C AppDelegate.h
and AppDelegate.m
, as Swift does not require separate headers and implementations, but rather a single .swift file.
However, under the hood, there are other key differences between the two.
In Objective-C, there exists a main.m
file that's sole purpose is instantiating UIApplicationMain
In Swift, the @UIApplicationMain
annotation tag found at the top of AppDelegate.swift replaces the need for any main function that existed in the main.m file in Objective-C. If this tag is omitted, you can use a main.swift file to instantiate your UIApplication
using the specified App Delegate.
A main.swift implementation looks like this:
import UIKit
autoreleasepool {
UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}
这篇关于AppDelegate.swift 如何替换 Xcode 6.3 中的 AppDelegate.h 和 AppDelegate.m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!