Swift中的子类NSApplication

我创建了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.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...