gpt4 book ai didi

swift - 如何检测用户何时更改 macOS 上的空间?

转载 作者:行者123 更新时间:2023-11-28 13:37:28 27 4
gpt4 key购买 nike

我正在尝试让 macOS 应用程序检测用户何时切换 Spaces。

主要遵循这个建议 ( Detecting when a space changes in Spaces in Mac OS X ) 虽然更新为

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {

NotificationCenter.default.addObserver(self, selector: #selector(self.spaceChange), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}

@objc func spaceChange() {
print("space did change")
}

}

这看起来不错,至少它编译时没有警告/错误。

当我切换 Spaces 时,我希望在应用程序运行时将“space did change”打印到日志中。但是,它永远不会打印到日志中。

最佳答案

是的,这里的问题是我用错了NotificationCenter,我们需要:

NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.spaceChange), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)

关于swift - 如何检测用户何时更改 macOS 上的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56435064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com