简体   繁体   English

未调用NSApplicationDelegate的applicationDidFinishLaunching

[英]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.

相关问题 有没有办法监听 NSApplicationDelegate applicationDidFinishLaunching 和 appplicatonWillTerminate 调用? - Is there a way to listen for the NSApplicationDelegate applicationDidFinishLaunching and applicatonWillTerminate calls? UICollectionView的prefetchItemsAt不被调用 - UICollectionView's prefetchItemsAt not being called 为什么在iOS Simulator中没有调用LocationManager,而是在电话中调用它? - Why is LocationManager not being called in iOS Simulator, but it's being called on the phone? 没有调用UIViewController为segue方法做准备 - UIViewController's prepare for segue method is not being called 为什么要调用此属性的didSet - Why is this property's didSet being called 从命令行构建的Swift 4 MacOS应用未调用AppDelegate#applicationDidFinishLaunching - AppDelegate#applicationDidFinishLaunching not called for Swift 4 MacOS app built from command line Swift的UITextView的shouldChangeTextInRange方法没有被调用吗? - Swift's UITextView's shouldChangeTextInRange method not being called? 没有Storyboard,NSApplicationDelegate无法正常工作 - NSApplicationDelegate not working without Storyboard 当集合视图中有 uibutton 时,不会调用 DidselectionItemAt - DidselectionItemAt not being called when there's a uibutton inside a collection view iOS 14 StoreKit - SKCloudServiceController 的 requestusertoken 没有被正确调用 - iOS 14 StoreKit - SKCloudServiceController's requestusertoken not being called properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM