问题描述
我的一个名为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
可以是nil
或object
(在此示例中,类似于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?
推荐答案
该对象"参数为" addObserver "是可选的过滤器.发布通知后,您可以将对象设置为通知的发件人,然后仅收到该发件人的事件的通知.如果将其设置为"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`是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!