gpt4 book ai didi

swift - AppDelegate.swift如何替换Xcode 6.3中的AppDelegate.h和AppDelegate.m

转载 作者:搜寻专家 更新时间:2023-10-30 22:08:59 25 4
gpt4 key购买 nike

根据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.

但是,在 Xcode 6.3 中似乎只有 AppDelegate.swift 而不再有 .h 和 .m 扩展名。我想了解 .swift 如何替换 .h 和 .m 扩展名。

最佳答案

简单的答案是 AppDelegate.swift 只是 Objective-C AppDelegate.hAppDelegate.m 的翻译,因为 Swift 不需要单独的 header 和实现,而是单个 .swift 文件。

然而,在本质上,两者之间还有其他关键区别。

在 Objective-C 中,存在一个 main.m 文件,其唯一目的是实例化 UIApplicationMain

在 Swift 中,位于 AppDelegate.swift 顶部的 @UIApplicationMain 注释标记取代了对 Objective-C 中 main.m 文件中存在的任何 main 函数的需要。如果省略此标记,您可以使用 main.swift 文件使用指定的 App Delegate 实例化您的 UIApplication

一个 main.swift 实现看起来像这样:

import UIKit

autoreleasepool {
    UIApplicationMain(Process.argc, Process.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))
}

关于swift - AppDelegate.swift如何替换Xcode 6.3中的AppDelegate.h和AppDelegate.m,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com