[英]Swift 3, NotificationCenter observer missing posted Notification
Swift 3中的NotificationCenter似乎有一些变化,我似乎无法完全理解它。
使用:
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
我有一个单例对象:
class Notifications {
private static let pipeline = Notifications()
...
接收和排队订阅NotificationsPipelineProtocol
项目。 (它们都是纯粹的快速,这里没有Objective-C NSObject。)
private func enqueueNotification(_ notification: NotificationsPipelineProtocol) {
...
其中它将自己添加为NotificationCenter的观察者
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: notification)
注 - notification.completionNotificationName
是一个生成Notification.Name
项的计算变量。
但是当NotificationsPipelineProtocol
项目发布到NotificationCenter时:
NotificationCenter.default.post(name: self.completionNotificationName, object: self)
观察者不会调用它的相关订阅方法:
@objc private func didReceiveNotificationCompletion(_ notification : Notification) {
...
你知道为什么吗? 有没有办法检查在NotificationCenter中查看特定项目订阅的通知? 也许是单身对象放弃它的观察? 也许#selector格式不正确?
XCode没有给我任何警告或错误。
提前致谢。
您正在将NotificationPipelinesProtocol
对象传递给addObserver
。 这意味着您只会收到该对象发布的通知。 如果要接收任何对象发布的指定名称的通知,则应传递nil
:
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.