【问题标题】:Where the AppDelegate get set as a delegate for the application?AppDelegate 在哪里设置为应用程序的委托?
【发布时间】:2021-02-26 18:06:55
【问题描述】:

环境、Swift 5.3 和 Xcode 12

通常我们使用someObject.delegate = self 将当前类设置为某个类实例的委托。但是,在 AppDelegate 内部,没有这样的分配使其成为应用程序的委托。

import Cocoa

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {}

    func applicationWillTerminate(_ aNotification: Notification) {}
}

那么应用程序的delegate 属性在哪里设置?

【问题讨论】:

    标签: ios swift macos swift5 xcode12


    【解决方案1】:

    @UIApplicationMain 属性表示该类是应用程序委托https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID589

    与调用UIApplicationMain(:,:,:,:) 相同。如果未使用该属性,请提供一个 main.swift 文件,其中包含调用 UIApplicationMain(:,:,:,:) 的顶级代码(也适用于 NSApplication)。

    添加@main 似乎与@UIApplicationMain 具有相同的效果。将@main 替换为@UIApplicationMain,应用运行良好,经过测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多