What is parameter `object` in NSNotification addObserver:?

tranvutuan picture tranvutuan · Jul 25, 2012 · Viewed 14.9k times · Source

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 will be be the receiver to catch the notification as follow

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

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

Question:

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

Answer

Thilo picture Thilo · Jul 25, 2012

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