gpt4 book ai didi

swift - 是否有一个 main.swift 相当于 @NSApplicationMain 注释?

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

在 XCode 中创建一个新的 Cocoa 项目会给我一个 AppDelegate.swift 文件,如下所示:

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
}

The @NSApplicationMain attribute is documented here作为

NSApplicationMain

Apply this attribute to a class to indicate that it is the application delegate. Using this attribute is equivalent to calling the NSApplicationMain(_:_:) function.

If you do not use this attribute, supply a main.swift file with code at the top level that calls the NSApplicationMain(_:_:) function as follows:

import AppKit
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

文档中的说明不起作用:AppDelegate 类从未实例化。在 this answer , vadianmain.swift 建议以下内容,它比文档中的代码更好:

import Cocoa
let appDelegate = AppDelegate()
NSApplication.shared().delegate = appDelegate
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

但是,这仍然没有提供与 @NSApplicationMain 相同的行为。考虑将上面的 main.swift 与以下 AppDelegate.swift 一起使用:

import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var foo: NSStatusBar! = NSStatusBar.system();
}

上面的 AppDelegate.swift 使用 @NSApplicationMain 注释,但是当使用上面的 main.swift 时,它在运行时失败了错误

Assertion failed: (CGAtomicGet(&is_initialized)), function CGSConnectionByID, file Services/Connection/CGSConnection.c, line 127.

我认为这个 is_initialized 错误意味着 @NSApplicationMain 进行了设置,以便 AppDelegate 之后实例化NSApplicationMain 函数的一些初始化。这建议使用以下 main.swift,它将委托(delegate)初始化移动到 NSApplicationMain 调用之后:

import Cocoa
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
let appDelegate = AppDelegate()
NSApplication.shared().delegate = appDelegate

但是,这也行不通,因为 NSApplicationMain 永远不会返回!上面的 main.swift 相当于文档中的 broken suggestion,因为后两行是死代码。

因此我认为必须有一些方法可以将对我的 AppDelegate 类的引用作为参数传递给 NSApplicationMain 函数,这样 Cocoa 就可以进行初始化,然后实例化我的 AppDelegate 类本身。但是,我看不出有什么办法。

是否有 main.swift 提供真正等同于 @NSApplicationMain 注释的行为?如果是这样,那 main.swift 是什么样子的?如果不是,@NSApplicationMain 实际在做什么,我该如何修改它?

最佳答案

文档假设有一个 xib 或 Storyboard通过 Interface Builder 中的对象(蓝色立方体)实例化 AppDelegate 类。在这种情况下都是

  • main.swift 包含 NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

  • @NSApplicationMainAppDelegate 类中

行为完全相同。

如果没有 xib 或 Storyboard,您负责初始化 AppDelegate 类,将其分配给 NSApplication.shared.delegate 并运行应用程序。您还必须考虑对象的出现顺序。例如,您不能在调用 NSApplication.shared 启动应用程序之前初始化与 AppKit 相关的对象。


例如语法略有改变

let app = NSApplication.shared
let appDelegate = AppDelegate()
app.delegate = appDelegate
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

您可以在 applicationDidFinishLaunching 之外的 AppDelegate 中初始化状态栏:

let statusItem = NSStatusBar.system().statusItem(withLength: -1)

因为启动应用程序的 NSApplication.shared() 初始化 AppDelegate 类之前被调用。

关于swift - 是否有一个 main.swift 相当于 @NSApplicationMain 注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42875741/

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