我們正常使用NSNotification進行消息傳遞的時候隔嫡,附帶參數(shù)都是通過userInfo來實現(xiàn)。
但是最近試了一下贯被,例如我想傳個BOOL類型的參數(shù)鞭光,試著用object來傳遞也是可以實現(xiàn)的。
試了以下幾種情況:
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這個參數(shù)并不是用來做參數(shù)傳遞的。翻了一下官方文檔:官方解釋最后一個參數(shù)為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參數(shù)做限制近哟,但是正確使用NSNotification的姿勢應(yīng)該是所有想進行傳遞的參數(shù)都應(yīng)該放在userInfo中,object只作為收發(fā)通知的一個限制要求鲫寄。