加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift中的子类NSApplication

发布时间:2020-12-14 05:28:43 所属栏目:百科 来源:网络整理
导读:我创建了NSApplication子类: class MyApplication: NSApplication { override func sendEvent(theEvent: NSEvent) { if theEvent.type == NSEventType.KeyUp (theEvent.modifierFlags .CommandKeyMask).rawValue != 0 { self.keyWindow?.sendEvent(theEvent
我创建了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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读