What is parameter `object` in NSNotification addObserver:?

百般思念 提交于 2019-11-30 06:23:55
Thilo

That "object" parameter to "addObserver" is an optional filter. Upon posting a notification you can set an object to the sender of the notification, and will then only be notified of that sender's events. If set to "nil" you will get all notification of this type (regardless who sent them).

You can use it to pass any object with the notification. The receiver of the notification will then be able to access that object. For example, you could implement dataReloaded like this:

- (void)dataReloaded:(NSNotification *)notification {

    NSLog(@"%@", notification.object); // this will log the object you passed in addObserver:selector:name:object:

}

It can be useful when you want to pass on data with your notification, so that the receiver of a notification can use that data too.

For anyone interested in apple's documentation. This is what it says:

notificationSender

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!