0

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()
ios coder
  • 1
  • 4
  • 31
  • 91

1 Answers1

0

The application class used by the app in a new Xcode project is loaded from the Info.plist file. You can set a different class by going to your target settings, the Info tab, and replacing the value for Principal class.

By default it should be NSApplication, and when using Swift you should make sure to set the module/target as a prefix.

Example

Keep in mind that NSApplication.shared will still be annotated as NSApplication, so you'll have to cast it to your own class:

let application = NSApplication.shared as? MyApplicationClass

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • I took all steps but still not working, I used this string for `Principal class` -> `CustomNSApplication.MySubNSApplication` and it does not works, I update the codes I used at the end. – ios coder Mar 21 '23 at 22:31