15

I created NSApplication subclass:

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)
        }
    }
}

After that, I changed "Principal class" in Info.plist to MyApplication and at the end in the Application Scene in Main.storyboard, I changed Application to MyApplication, too.

When I run application I get following message:

Unable to find class: MyApplication, exiting

Can someone help me with this?

pkamb
  • 33,281
  • 23
  • 160
  • 191
iPera
  • 1,053
  • 1
  • 12
  • 24

1 Answers1

39

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.

rintaro
  • 51,423
  • 14
  • 131
  • 139
  • I found I needed the `@objc(MyClassName)` line and with that, the NSPrincipalClass value had to be `MyClassName` on Xcode 7.2 for 10.11 SDK. Nothing else seemed to work. – uchuugaka Dec 19 '15 at 10:20
  • Well, the other approach DOES work with just name spacing, IF you can figure out the namespace. If the app name has a space in it, you need an underscore there. Who knows if it is non-English. – uchuugaka Dec 19 '15 at 10:22
  • 1
    As of Xcode 11.3, I can confirm that $(PRODUCT_NAME).MyClass works with a Swift file. It's important not to add the extension. – Alex Zavatone Jan 17 '20 at 17:19