AppDelegate#applicationDidFinishLaunching not call

2019-08-23 09:38发布

So here is the full code:

main.swift

import Cocoa

let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

AppDelegate.swift

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
  override init() {
    Swift.print("AppDelegate.init")
    super.init()
    Swift.print("AppDelegate.init2")
  }

  func applicationDidFinishLaunching(aNotification: NSNotification) {
    Swift.print("AppDelegate.applicationDidFinishLaunching")
  }

  func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
    Swift.print("AppDelegate.applicationShouldTerminateAfterLastWindowClosed")
  }
}

Then I compile it with:

swiftc Sources/*
./main

It logs:

AppDelegate.init
AppDelegate.init2

But then it doesn't log anything else, so I close it with CTRL+C. Not sure why it's not reaching the applicationDidFinishLaunching method ever. Wondering if one knows of a fix for this, it seems like there is just one method that needs to be called somewhere, but I'm not sure.

I think this may be causing other issues such as NSMenu not working.

1条回答
别忘想泡老子
2楼-- · 2019-08-23 09:56

Hooray, it was just because of the format of the methods, which were silently failing I guess.

func applicationWillFinishLaunching(_ notification: Notification) {
  Swift.print("AppDelegate.applicationWillFinishLaunching")
}

func applicationDidFinishLaunching(_ notification: Notification) {
  Swift.print("AppDelegate.applicationDidFinishLaunching")
}
查看更多
登录 后发表回答