0

Here is the code in main.m

[[NSNotificationCenter defaultCenter]addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];

here is the code in .h file

-(void)zoneChange:(NSNotification *)note;

Can someone tell me why zoneChange method take NSNotification as an argument? How do we know what argument does this method take when trying to declare it to be used by the method mentioned in the main.m file above?

Also I did some research on the class reference and found out this for the selector parameter

Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

Please explain. Thanks

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user3090658
  • 157
  • 2
  • 9

2 Answers2

3

Imagine you have a controller that has two table views:

@interface MyController : NSViewController <NSTableViewDelegate>

@property(nonatomic,weak) IBOutlet NSTableView *tableView1;
@property(nonatomic,weak) IBOutlet NSTableView *tableView2;

@end

And controller is set as delegate for both table views. Now you want to track changes in selections of both tables. You implement tableViewSelectionDidChange: delegate method, but when it is called, how do you know which table view did change it's selection? Here comes the notification argument. It's object property points to table view that did sent this notification, and you can easily differentiate between two. Also it may contain userInfo dictionary, for example NSTableViewColumnDidResizeNotification contains a NSTableColumn and NSOldWidth keys in there.

In described case, the method responding to notification was called under delegate idiom, but in other scenarios (observed notification, target-action, etc.) you also sometimes must differentiate objects that caused a method call.

@implementation MyController

...

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    if ([notification object] == self.tableView1)
        NSLog(@"selection changed in table 1");

    else if ([notification object] == self.tableView2)
        NSLog(@"selection changed in table 2");

    else
        NSLog(@"selection changed in unknown table (???)");
}

- (void)buttonDidClick:(id)sender
{
    NSLog(@"some button did click. which? %@", sender);
}

@end
user3125367
  • 2,920
  • 1
  • 17
  • 17
2

Well, this is what the api states

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

where notificationSelector is a

Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

So, most of the time, you would go and read Apple provided documentations to see what kind of arguments are available for your selector.

Source: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Infinity
  • 3,695
  • 2
  • 27
  • 35
  • So does that mean I don't really have to know why they need NSNotification argument but just write the code according to what argument they need? – user3090658 Feb 16 '14 at 07:59
  • @user3090658 "They" don't need it. You do, because notification contains at least notifying `object` and `userInfo` properties. That's why target-action mechanism always sends `sender` to target's action. – user3125367 Feb 16 '14 at 08:05
  • @user3125367 I don't really understand what you meant could you please elaborate further? – user3090658 Feb 16 '14 at 08:07
  • Remember that you are defining the method so the NSNotification is for you to you. You don't have to use it. In fact, you only have to do this @selector(zoneChange) if you dont need the notification object. – Infinity Feb 16 '14 at 11:00
  • user3125367 and user3090658.. Have we met somewhere? You seem familiar... hint hint, wink wink, nudge nudge.. – ICL1901 Mar 29 '14 at 21:23