Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSApplicationDelegate's applicationDidFinishLaunching is not being called

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
like image 379
Allactaga Avatar asked Sep 03 '16 23:09

Allactaga


1 Answers

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

like image 80
OOPer Avatar answered Oct 12 '22 05:10

OOPer