I am trying to make sub class of NSApplication with this code:
class MySubNSApplication: NSApplication {
static var internalShared: MySubNSApplication = MySubNSApplication()
static override var shared: MySubNSApplication {
get {
return internalShared
}
set(newValue) {
internalShared = newValue
}
}
}
And it seems the codes are ok, but I have issue to make use of it, the normal coding for super class would be:
let nsApplication: NSApplication = NSApplication.shared
And if want make use of MySubNSApplication, that would be this below codes in a main file:
import Cocoa
let myNSApplication: MySubNSApplication = MySubNSApplication.shared
let appDelegate: AppDelegate = AppDelegate()
myNSApplication.delegate = appDelegate
myNSApplication.run()
Which the error happens on line of myNSApplication.run():
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
How can I solve this error?
update:
import Cocoa
let myNSApplication = NSApplication.shared as? MySubNSApplication
let appDelegate: AppDelegate = AppDelegate()
myNSApplication?.delegate = appDelegate
myNSApplication?.run()
The application class used by the app in a new Xcode project is loaded from the
Info.plistfile. You can set a different class by going to your target settings, the Info tab, and replacing the value forPrincipal class.By default it should be
NSApplication, and when using Swift you should make sure to set the module/target as a prefix.Keep in mind that
NSApplication.sharedwill still be annotated asNSApplication, so you'll have to cast it to your own class:let application = NSApplication.shared as? MyApplicationClass