NSNotification object

我们正常使用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只作为收发通知的一个限制要求。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容