5

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
jscs
  • 63,694
  • 13
  • 151
  • 195
Allactaga
  • 193
  • 1
  • 7
  • 2
    As far as I know, `NSApplicationDelegate` does not define a method `application(_:didFinishLaunchingWithOptions:)`. Aren't you confusing `NSApplicationDelegate.applicationDidFinishLaunching(_:)` with `UIApplicationDelegate.application(_:didFinishLaunchingWithOptions:)`? – OOPer Sep 03 '16 at 23:26
  • Thank you for pointing this out. I've made changes to the question. Effects are unfortunatelly still the same – Allactaga Sep 03 '16 at 23:52

3 Answers3

4

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

OOPer
  • 47,149
  • 6
  • 107
  • 142
  • For example: `main.swift:229:10: warning: instance method 'applicationDidFinishLaunching' nearly matches optional requirement 'applicationDidFinishLaunching' of protocol 'NSApplicationDelegate' func applicationDidFinishLaunching(_ aNotification: NSNotification) { ^` – Allactaga Sep 04 '16 at 01:10
  • 1
    Try dropping the NS from NSNotification – Leo Dabus Sep 04 '16 at 01:12
  • 1
    That's answers the question I've asked, but interestingly, don't solve my issue with opening window. I'll play around and probably ask another question later. – Allactaga Sep 04 '16 at 01:16
  • To be honest I don't really "want" to implement Objective-C protocols. What I am trying to do is to write a minimal gui app in a "swift 3 way", but searching for any docs I either find swift2 or iOS related articles, which I cannot find a way to adapt – Allactaga Sep 04 '16 at 01:20
3

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

aToz
  • 2,463
  • 1
  • 24
  • 34
0

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.

R. Rincón
  • 365
  • 2
  • 13