There seems to be a few changes surrounding the NotificationCenter in Swift 3 and I can't seem to quite get it right.
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
. (They are all pure swift, no Objective-C NSObjects here.)
private func enqueueNotification(_ notification: NotificationsPipelineProtocol) {
...
in which it adds itself as an observer to the 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.
But when the NotificationsPipelineProtocol
item posts to the 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? Is perhaps the singleton object dropping it's observation? Maybe the #selector has been improperly formatted?
XCode gives me no warnings or errors.
Thanks in advance.
You are passing the NotificationPipelinesProtocol
object to 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
:
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: nil)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.