0
- (void)postNotificationName:(NSString *)notificationName 
                      object:(id)notificationSender

Can someone help me understand the object parameter in the above method?

I have used

[[NSNotificationCenter defaultCenter] postNotificationName:@"Downloadfinished"
                                                    object:self]; 

and

[[NSNotificationCenter defaultCenter] postNotificationName:@"Downloadfinished"
                                                    object:nil];

They both are working in my case. But I want to understand what the argument does and what I should be passing.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Nitya
  • 849
  • 1
  • 11
  • 25
  • possible duplicate of [how to use the object property of NSNotificationcenter](http://stackoverflow.com/questions/4312338/how-to-use-the-object-property-of-nsnotificationcenter) – BhavikKama Oct 11 '13 at 10:54

3 Answers3

3

From the documentation:

notificationSender 
The object posting the notification.

That's all, you may need it or you may not. If you are not using it when you receive the notification, the it doesn't matter if it's nil or not.

check the documentation:

NSNotificationCenter

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • 1
    I would say that sending `self` when you're not using this parameter could be confusing for someone trying to understand the code. If message is only information that something happened and you don't need context for it - send `nil`. For the purpose of clear intention. – Tomasz BÄ…k Oct 11 '13 at 11:01
1

NSNotification has the following three attributes:

  1. name - the unique identifier for notification.
  2. object - an id parameter, which can be passed to the receiver, and can be used for whatever purpose at receiving end, if required
  3. userInfo - NSDictionary object: in case you want to pass multiple objects, make an NSDictionary with the key/value pairs, and pass it on.

If you don't want to pass anything to the receiver, pass nil for object.

blackzero
  • 78
  • 7
0

Case: Self

When you write object as Self or any other Object then it means the notification will fire with the object means pass the Object as a parameter of the notification.

you will get the object as follow:

Example

[[NSNotificationCenter defaultCenter] postNotificationName:kProductsLoadedNotification object:self];

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(productsRequestCompleted:)
                                                     name:kProductsLoadedNotification
                                                   object:self];
- (void)productsRequestCompleted:(NSNotification *)notification
{
NSLog("%@",[notification object]); //You will get the Parameter
}

When

Case: nil

When you write object as nil then it means the notification will fire without the object means without pass the Object as a parameter of the notification.

user1673099
  • 3,293
  • 7
  • 26
  • 57