NSNotification addObserver中的参数`object`是什么? [英] What is parameter `object` in NSNotification addObserver:?

查看:115
本文介绍了NSNotification addObserver中的参数`object`是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个名为Message.m的类正在发布一个带有对象sentObject的通知,如下所示:

One of my class named Message.m is posting a notification with an object sentObject as below

NSDictionary *sentObject = [NSDictionary dictionaryWithObjectsAndKeys:draftData.arr,@"data", nil];

//Post notification to inform a receiver to reload data     
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadDuringSave" object:self userInfo:sentObject];

DraftData.m将成为接收通知的接收者,

DraftData.m will be be the receiver to catch the notification as follow

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(dataReloaded:) 
                                             name:@"reloadDuringSave" 
                                           object:nil];

对于发布通知,userInfo可以是nilobject(在此示例中,类似于sentObject,其类型为NSDictionary).

For posting notification, userInfo can be nil or can be an object (like sentObject as type of NSDictionary in this example).

addObserver方法中的object的其他参数是什么?他们可以吗 nil以外的其他东西,如果是的话,怎么办?

What are other params for object in addObserver method? Can they be anything other than nil, and if so what?

推荐答案

该对象"参数为"过滤器.发布通知后,您可以将对象设置为通知的发件人,然后仅收到该发件人的事件的通知.如果将其设置为"nil",则将收到此类型的所有通知(无论是谁发送的).

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).

这篇关于NSNotification addObserver中的参数`object`是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆