2017-06-07 96 views
2

在斯威夫特3我註冊了睡眠和喚醒的通知與此代碼:如何將NSWorkspace通知遷移到Swift 4?

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) 

但遷移到斯威夫特4後,我將建議的修正後得到這個錯誤:

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

我如何在Swift 4中做這個?

回答

4

要解決這個問題,您只需要將引用通知名稱的代碼從NSNotification.Name.NSWorkspaceWillSleep調整爲NSWorkspace.willSleepNotification即可。雨燕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) 

你可以看到蘋果的API的diff這種變化here