簡體   English   中英

如何將 NSWorkspace 通知遷移到 Swift 4?

[英]How to migrate NSWorkspace notifications to Swift 4?

在 Swift 3 中,我使用以下代碼注冊了睡眠和喚醒通知:

let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(self, selector: #selector(AppDelegate.sleepListener), name: NSNotification.Name.NSWorkspaceWillSleep, object: nil)
notificationCenter.addObserver(self, selector: #selector(AppDelegate.wakeUpListener), name: NSNotification.Name.NSWorkspaceDidWake, object: nil)

但是在遷移到 Swift 4 后,我在應用建議的修復程序后收到此錯誤:

Type 'NSNotification.Name' has no member 'NSWorkspace'

我如何在 Swift 4 中做到這一點?

要解決此問題,您需要將引用通知名稱的代碼從NSNotification.Name.NSWorkspaceWillSleepNSWorkspace.willSleepNotification Swift 4 版本是:

let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(self, selector: #selector(AppDelegate.sleepListener), name: NSWorkspace.willSleepNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(AppDelegate.wakeUpListener), name: NSWorkspace.didWakeNotification, object: nil)

您可以在此處查看 Apple 對此更改的 API 差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM