[英]AppDelegate#applicationDidFinishLaunching not called for Swift 4 MacOS app built from command line
所以這是完整的代碼:
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")
}
}
然后我用以下代碼進行編譯:
swiftc Sources/*
./main
它記錄:
AppDelegate.init
AppDelegate.init2
但是隨后它不記錄其他任何內容,因此我使用CTRL + C將其關閉。 不知道為什么它沒有達到applicationDidFinishLaunching
方法。 想知道是否有人知道解決方法,似乎只有一種方法需要在某處調用,但我不確定。
我認為這可能會導致其他問題,例如NSMenu無法正常工作。
太好了,這僅僅是因為方法的格式,我猜這是默默地失敗了。
func applicationWillFinishLaunching(_ notification: Notification) {
Swift.print("AppDelegate.applicationWillFinishLaunching")
}
func applicationDidFinishLaunching(_ notification: Notification) {
Swift.print("AppDelegate.applicationDidFinishLaunching")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.