2017-04-15 21 views
1

Swift 3のNotificationCenterを取り巻くいくつかの変更があるようで、私はそれを正しく理解できないようです。使用Swift 3、NotificationCenterオブザーバが表示されない通知がある

class Notifications { 

    private static let pipeline = Notifications() 
    ... 

受け取り、NotificationsPipelineProtocolに加入項目をエンキュー:

Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1) 

を私はシングルトンオブジェクトを持っています。 は(彼らは、客観-C NSObjectsここで全ての純粋な迅速である。)

private func enqueueNotification(_ notification: NotificationsPipelineProtocol) { 
     ... 

は、それは、通知センター

 NotificationCenter.default.addObserver(self, 
             selector: #selector(Notifications.didReceiveNotificationCompletion(_:)), 
             name: notification.completionNotificationName, 
             object: notification) 

にオブザーバーとしてそれ自体を追加 - notification.completionNotificationName計算されます。 Notification.Nameアイテムを生成する変数。

しかしとき通知センターにNotificationsPipelineProtocolアイテムポスト:

NotificationCenter.default.post(name: self.completionNotificationName, object: self) 

観察者が呼び出すことはありません、それに関連付けられている加入方法:

@objc private func didReceiveNotificationCompletion(_ notification : Notification) { 
    ... 

あなたはなぜ知っているかもしれませんか? NotificationCenterで特定のアイテムが購読されている通知を確認する方法はありますか?おそらくシングルトンオブジェクトがそれを観測しているのでしょうか?多分、#セレクタのフォーマットが間違っていますか?

XCodeでは、警告またはエラーは表示されません。

ありがとうございます。

+0

'enqueueNotification'と呼ばれるどこ? 'enqueueNotification'の' notification'は、あなたが観察したいすべての通知を掲示しているオブジェクトですか?おそらく 'object'パラメータを' nil'にしますか? – Paulw11

+0

私はNotificationsのキューとも呼ばれるパイプラインを構築してパイプラインに存在することを確認しました。オブジェクトへの参照を含まずに試してみます – achi

+0

それはうまくいったのですか?あなたが答えとして投稿した場合、私はそれを受け入れるでしょう:) – achi

答えて

3

NotificationPipelinesProtocolオブジェクトをaddObserverに渡しています。つまり、そのオブジェクトによって通知された通知は、のみとなります。あなたがによって任意のオブジェクト投稿を、指定した名前の通知を受信したいなら、あなたはnilを渡す必要があります。

NotificationCenter.default.addObserver(self, 
             selector: #selector(Notifications.didReceiveNotificationCompletion(_:)), 
             name: notification.completionNotificationName, 
             object: nil) 
+0

これは、 'addObserver(forName:object:queue:using:)'バリエーションを使って私を引きつけました。オブザーバー。 – clozach

関連する問題