我們正常使用NSNotification進行消息傳遞的時候,附帶參數都是通過userInfo來實現。
但是最近試了一下,例如我想傳個BOOL類型的參數,試著用object來傳遞也是可以實現的。
試了以下幾種情況:
1.post填nil, add填XXX。此時收不到通知。
2.post填XXX, add填nil。此時收到通知。
1.post填XXX, add填XXX。此時收到通知。
2.post填nil, add填nil。此時收到通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(XXXX:) name:@"XXXXX" object:objectA];
[[NSNotificationCenter defaultCenter] postNotificationName:@"XXXXX" object:objectA userInfo:@{@"key":@"param"}];
可是按道理object這個參數并不是用來做參數傳遞的。翻了一下官方文檔:官方解釋最后一個參數為notificationSender,解釋如下:
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
官方并沒有對object參數做限制,但是正確使用NSNotification的姿勢應該是所有想進行傳遞的參數都應該放在userInfo中,object只作為收發通知的一個限制要求。