NSApplicationDelegate's applicationDidFinishLaunching is not being called

5k views Asked by At

I've been trying to play with Swift 3, but I am unable to get started. The following code is compiling, however it does not log anything. Looks like applicationDidFinishLaunching is not being called. Am I missing some critical piece here?

Sources/main.swift:

import AppKit

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        NSLog("application start")
    }
}

NSApplication.shared()
NSApp.setActivationPolicy(.regular)
let controller = AppDelegate()
NSApp.delegate = controller
NSApp.run()

p.s. There is a similar question about applicationDidFinishLaunching being called but not printing anything. I believe that this is not the case, as having window.orderFrontRegardless() instead of logging also has no effect for me.

System Version: OS X 10.11.6

> swift --version
Apple Swift version 3.0 (swiftlang-800.0.43.6 clang-800.0.38)
Target: x86_64-apple-macosx10.9
4

There are 4 answers

4
OOPer On BEST ANSWER

If you want to implement Objective-C protocols in Swift 3, you need to use _ indicating the method has no label for the first parameter.

func applicationDidFinishLaunching(_ aNotification: Notification) {

(UPDATE)

Sorry, in my first code, I have forgotten to to replace NSNotification with Notification. (Thanks, Leo Dabus.)

0
R. Rincón On

The line where you refer to, but don't use NSApplication.shared looks like a typo. If you replace it with NSApp = NSApplication.shared, and make sure the method signature in AppDelegate is correct it, should work.

0
aToz On

I'd a similar problem where func applicationDidFinishLaunching(_ aNotification: Notification) was not getting called.

For me, the issue was with the App Delegate referencing outlet.

enter image description here

Solution: Just removing it and then connecting it back had resolved the issue for me.

This is how you can recreate a connection

enter image description here

2
Paul Stevenson On

for me... I had no windows visible at launch. As soon as I ticked that box on one of the windows, applicationDidFinishLaunching was called when I ran the app.