Dan*_*ton 2 nsnotifications nsnotification nsworkspace swift swift4
在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)
Run Code Online (Sandbox Code Playgroud)
但在迁移到Swift 4后,我在应用建议的修复程序后出现此错误:
Type 'NSNotification.Name' has no member 'NSWorkspace'
Run Code Online (Sandbox Code Playgroud)
我如何在Swift 4中执行此操作?
要解决此问题,您只需调整引用通知名称的代码NSNotification.Name.NSWorkspaceWillSleep即可NSWorkspace.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)
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看Apple的API差异以进行此更改.
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |