29

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?

pkamb
  • 33,281
  • 23
  • 160
  • 191
tranvutuan
  • 6,089
  • 8
  • 47
  • 83

3 Answers3

54

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

mfaani
  • 33,269
  • 19
  • 164
  • 293
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Upvote, as this answer is more accurate than mine. While my method can work if filtering is not required, the userInfo parameter is really used for passing data. – Scott Berrevoets Jul 25 '12 at 06:44
  • how can i set `object` to the sender ( in this example is Message.m, please see my updated OP ). – tranvutuan Jul 25 '12 at 06:45
  • Well, you need a reference (variable) pointing to it. But note that this won't make any difference unless you have multiple objects sending the "reloadDuringSave" notification. – Thilo Jul 25 '12 at 06:48
  • I have 2 options to create a reference to Message.m : (a) Message *message = [[Message alloc] init] and (b) Message *message = [[Message alloc] initWithNibName@"MessageView"] at DraftData.m ; i doubt both ways to create a reference and pass it to `object`. However, the receiver does not catch a notification from sender (Message.m class) – tranvutuan Jul 25 '12 at 06:54
  • 4
    You have to use the *same object* that you pass as "object" when posting the notification. Not just another object of the same class. – Thilo Jul 25 '12 at 06:57
  • 2
    yeah I understood that i have to pass exactly the same object which posts a noti and honestly, I have no idea how to do it :( – tranvutuan Jul 25 '12 at 07:00
  • And you are sure, you need the filter? It is optional after all. – Thilo Jul 25 '12 at 07:03
  • No i dont need the filter in this example. However, I want to understand deeply how to pass the same object which is the one posting notification – tranvutuan Jul 25 '12 at 07:06
  • "an optional filter" is a great way to say it. The Apple docs on this should be better and more like this. – Donn Lee Apr 26 '16 at 17:40
4

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.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • 1
    yup I am passing data with my notification. In `addOberver`, I a passing nil to object. If i dont pass nil, what should I pass – tranvutuan Jul 25 '12 at 06:38
  • It is perfectly fine to pass nil, if you don't need the data in `dataReloaded`. If you do need the data, then you pass whatever object contains that data. – Scott Berrevoets Jul 25 '12 at 06:39
  • actually, I do need tho data in `dataReload` but I dont know what I have to pass to `object` – tranvutuan Jul 25 '12 at 06:43
4

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.

Community
  • 1
  • 1
nomann
  • 2,257
  • 2
  • 21
  • 24