Subclass NSApplication in Swift

大城市里の小女人 提交于 2019-11-30 08:53:14

Try:

@objc(MyApplication)
class MyApplication: NSApplication {

OR, you have to set Info.plist like this.

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

There is no exact document about this, but 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.

and this:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!