当我切换应用程序时我的功能没有被执行:
public class ItemObserver: NSObject {
@objc public func recievedNotification(notification: NSNotification) {
print(notification.name)
print("s")
}
}
let observer = ItemObserver()
NSWorkspace.shared().notificationCenter.addObserver(observer, selector: #selector(ItemObserver.recievedNotification(notification:)), name: .NSWorkspaceDidActivateApplication, object: nil)
最佳答案
NSNotificationCenter 不保留观察者:它使用 zeroing weak reference , 在可能的情况。如果您记录它的 init
和 deinit
,您会看到它正在立即被释放。
保持对观察者的强烈引用。
关于swift - 未收到 NSWorkspace 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44468795/