gpt4 book ai didi

swift - Swift 中的子类 NSApplication

转载 作者:IT王子 更新时间:2023-10-29 05:29:00 26 4
gpt4 key购买 nike

我创建了 NSApplication 子类:

class MyApplication: NSApplication {
override func sendEvent(theEvent: NSEvent) {
if theEvent.type == NSEventType.KeyUp && (theEvent.modifierFlags & .CommandKeyMask).rawValue != 0 {
self.keyWindow?.sendEvent(theEvent)
} else {
super.sendEvent(theEvent)
}
}
}

之后,我将 Info.plist 中的“Principal class”更改为 MyApplication,最后在 Main.storyboard 的 Application Scene 中,我也将 Application 更改为 MyApplication。

当我运行应用程序时,我收到以下消息:

Unable to find class: MyApplication, exiting

有人可以帮我解决这个问题吗?

最佳答案

尝试:

@objc(MyApplication)
class MyApplication: NSApplication {

或者,您必须像这样设置 Info.plist。

<key>NSPrincipalClass</key>
<string>YourAppName.MyApplication</string>
^^^^^^^^^^^^

关于这个没有确切的文件,但是this description is corresponding :

In order to preserve namespacing when a Swift class is used in Objective-C code, Swift classes are exposed to the Objective-C runtime with their fully qualified names. Therefore, when you work with APIs that operate on the string representation of a Swift class, you must include the fully qualified name of the class. For example, when you create a document–based Mac app, you provide the name of your NSDocument subclass in your app’s Info.plist file. In Swift, you must use the full name of your document subclass, including the module name derived from the name of your app or framework.

this :

When you use the @objc(<#name#>) attribute on a Swift class, the class is made available in Objective-C without any namespacing.

关于swift - Swift 中的子类 NSApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27144113/

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