如何将NSWorkspace通知迁移到Swift 4? [英] How to migrate NSWorkspace notifications to Swift 4?

查看:254
本文介绍了如何将NSWorkspace通知迁移到Swift 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 3中,我使用以下代码注册了睡眠和唤醒通知:

In Swift 3 I registered for sleep and wake notifications with this code:

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之后,在应用建议的修复程序后出现了此错误:

but after migrating to Swift 4, I get this error after applying the suggested fixes:

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

如何在Swift 4中做到这一点?

How do I do this in Swift 4?

推荐答案

要解决此问题,您需要将引用通知名称的代码从NSNotification.Name.NSWorkspaceWillSleep调整为NSWorkspace.willSleepNotification. Swift 4版本是:

To fix this, you need to adjust the code referencing the notification's name from NSNotification.Name.NSWorkspaceWillSleep to NSWorkspace.willSleepNotification. The Swift 4 version is:

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)

您可以在此处.

这篇关于如何将NSWorkspace通知迁移到Swift 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆