0

Normally, I'm using nil in object parameter while adding observer in any class. Now, My Question is what is use of object parameter while adding observer, for posting, I can understand that, It's useful for pass any object as notification object.

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

Yes, This is bit silly question but please explain me if you have knowledge about it. So, I can't miss any advantage of passing object while adding observer.

As per this question's answer, I've still confusion. If it's for filtering only why it's not as boolean type and which kind of object it's require. If possible please explain me using some example.

Regards,

Sonu
  • 937
  • 1
  • 10
  • 39

1 Answers1

0

The object parameter is a filter: The observer method will only receive notifications when the sender of the notification is the given object. You may have multiple senders for a notification type. The object parameter identifies the sender. Setting the object to desiredSender is similar to

- (void)testmethod:(NSNotification *)notification {
    if(notification.object == desiredSender) {
        ...
    }
}

If you pass nil instead of an object notifications of all senders are received.

clemens
  • 16,716
  • 11
  • 50
  • 65
  • I've still confusion. If it's for filtering only why it's not as boolean type and which kind of object it's require. If possible please explain me using some example. – Sonu Feb 14 '18 at 08:46
  • A boolean is not enough. See my edit. – clemens Feb 14 '18 at 09:58