从命令行构建的Swift 4 MacOS应用未调用AppDelegate#applicationDidFinishLaunching [英] 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")
}
}
然后我用以下代码进行编译:
Then I compile it with:
swiftc Sources/*
./main
它记录:
AppDelegate.init
AppDelegate.init2
但是随后它不记录其他任何内容,因此我使用CTRL + C将其关闭.不知道为什么它没有达到applicationDidFinishLaunching
方法.想知道是否有人知道此问题的解决方法,似乎只有一种方法需要在某处调用,但我不确定.
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.
我认为这可能导致其他问题,例如NSMenu无法正常工作.
I think this may be causing other issues such as NSMenu not working.
推荐答案
万岁,这仅仅是因为方法的格式,我想这是默默地失败了.
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")
}
这篇关于从命令行构建的Swift 4 MacOS应用未调用AppDelegate#applicationDidFinishLaunching的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!