[英]NSApplicationDelegate's applicationDidFinishLaunching is not being called
I've been trying to play with Swift 3, but I am unable to get started. 我一直在尝试使用Swift 3,但无法入门。 The following code is compiling, however it does not log anything.
以下代码正在编译,但是不记录任何内容。 Looks like
applicationDidFinishLaunching
is not being called. 好像没有调用
applicationDidFinishLaunching
。 Am I missing some critical piece here? 我在这里错过了一些重要的作品吗?
Sources/main.swift : 资料来源/ 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()
ps There is a similar question about applicationDidFinishLaunching
being called but not printing anything. ps关于
applicationDidFinishLaunching
被调用但不打印任何内容,存在类似的问题。 I believe that this is not the case, as having window.orderFrontRegardless()
instead of logging also has no effect for me. 我相信事实并非如此,因为使用
window.orderFrontRegardless()
而不是日志记录对我也没有影响。
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
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. 如果要在Swift 3中实现Objective-C协议,则需要使用
_
表示该方法没有第一个参数的标签。
func applicationDidFinishLaunching(_ aNotification: Notification) {
(UPDATE) (更新)
Sorry, in my first code, I have forgotten to to replace NSNotification
with Notification
. 抱歉,在我的第一个代码中,我忘记了用
Notification
代替NSNotification
。 (Thanks, Leo Dabus.) (谢谢,Leo Dabus。)
The line where you refer to, but don't use NSApplication.shared
looks like a typo. 您引用但不使用
NSApplication.shared
看起来像是错字。 If you replace it with NSApp = NSApplication.shared
, and make sure the method signature in AppDelegate is correct it, should work. 如果将其替换为
NSApp = NSApplication.shared
,并确保AppDelegate中的方法签名正确,则应该起作用。
I'd a similar problem where func applicationDidFinishLaunching(_ aNotification: Notification)
was not getting called. 我会遇到类似的问题,其中未
func applicationDidFinishLaunching(_ aNotification: Notification)
。
For me, the issue was with the App Delegate referencing outlet . 对我而言,问题出在App Delegate引用插座 。
Solution: Just removing it and then connecting it back had resolved the issue for me.
解决方案:只需将其删除然后再重新连接,即可为我解决此问题。
This is how you can recreate a connection 这是您可以重新创建连接的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.