[英]Swift 3, NotificationCenter observer missing posted Notification
There seems to be a few changes surrounding the NotificationCenter in Swift 3 and I can't seem to quite get it right. Swift 3中的NotificationCenter似乎有一些变化,我似乎无法完全理解它。
Using: 使用:
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
I have a singleton object: 我有一个单例对象:
class Notifications {
private static let pipeline = Notifications()
...
That receives and enqueues items subscribing to NotificationsPipelineProtocol
. 接收和排队订阅
NotificationsPipelineProtocol
项目。 (They are all pure swift, no Objective-C NSObjects here.) (它们都是纯粹的快速,这里没有Objective-C NSObject。)
private func enqueueNotification(_ notification: NotificationsPipelineProtocol) {
...
in which it adds itself as an observer to the NotificationCenter 其中它将自己添加为NotificationCenter的观察者
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: notification)
NOTE - notification.completionNotificationName
is a computed variable that produces a Notification.Name
item. 注 -
notification.completionNotificationName
是一个生成Notification.Name
项的计算变量。
But when the NotificationsPipelineProtocol
item posts to the NotificationCenter: 但是当
NotificationsPipelineProtocol
项目发布到NotificationCenter时:
NotificationCenter.default.post(name: self.completionNotificationName, object: self)
The observer does not call it's associated subscribed method: 观察者不会调用它的相关订阅方法:
@objc private func didReceiveNotificationCompletion(_ notification : Notification) {
...
Might you know why? 你知道为什么吗? Is there a way to check to see in NotificationCenter to which notifications a particular item is subscribed to?
有没有办法检查在NotificationCenter中查看特定项目订阅的通知? Is perhaps the singleton object dropping it's observation?
也许是单身对象放弃它的观察? Maybe the #selector has been improperly formatted?
也许#selector格式不正确?
XCode gives me no warnings or errors. XCode没有给我任何警告或错误。
Thanks in advance. 提前致谢。
You are passing the NotificationPipelinesProtocol
object to addObserver
. 您正在将
NotificationPipelinesProtocol
对象传递给addObserver
。 This means that you will only receive notifications posted by that object . 这意味着您只会收到该对象发布的通知。 If you want to receive notifications of the specified name posted by any object then you should pass
nil
: 如果要接收任何对象发布的指定名称的通知,则应传递
nil
:
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.