Instance method 'applicationDidFinishLaunching' nearly matches optional

This is a swift project that has C bridged. As soon as the C was bridged I got the warning: Instance method 'applicationDidFinishLaunching' nearly matches optional requirement 'applicationDidFinishLaunching' of protocol 'NSApplicationDelegate'

When launched, the func applicationDidFinishLaunching is not called.

This is for MacOS 10.15. Using Xcode 12.3.
Answered by OOPer in 655061022

Here is the code copy and pasted as is: p.s. it is linked with Boost lib (C++)

Thanks for showing your code. Your method header is seemingly right.

Can you try changing the method header as follows and tell us what happens?
Code Block
func applicationDidFinishLaunching(_ aNotification: AppKit.Notification) { //<- Use `AppKit.Notification` instead of `Notification`


Please show your code where your applicationDidFinishLaunching exists. Something is wrong there.
The func signature is:
Code Block
    func applicationDidFinishLaunching(_ aNotification: Notification) { }

May be you forgot the underscore ?
If you remove the _, func which is optional, will never be called.
Here is the code copy and pasted as is: p.s. it is linked with Boost lib (C++)
Code Block
@main
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: NSWindow!
// Xcode warning here: Instance method 'applicationDidFinishLaunching' nearly matches optional requirement 'applicationDidFinishLaunching' of protocol 'NSApplicationDelegate'
//
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        print("FIX applicationDidFinishLaunching")
        let contentView = ContentView()
        // Create the window and set the content view.
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }
// Xcode warning here: Instance method 'applicationWillTerminate' nearly matches optional requirement 'applicationDidFinishLaunching' of protocol 'NSApplicationDelegate'
//
    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

Signature seems OK.

Could you post the complete class AppDelegate file ?

Did you try an option-clean build folder ?
Accepted Answer

Here is the code copy and pasted as is: p.s. it is linked with Boost lib (C++)

Thanks for showing your code. Your method header is seemingly right.

Can you try changing the method header as follows and tell us what happens?
Code Block
func applicationDidFinishLaunching(_ aNotification: AppKit.Notification) { //<- Use `AppKit.Notification` instead of `Notification`


Problem resolved. My error was I defined a "struct Notification" in the project which conflicts with Foundation "struct Notification". Thus the signature of the functions applicationDidFinishLaunching and applicationWillTerminate would not match!
Instance method 'applicationDidFinishLaunching' nearly matches optional
 
 
Q