Reputation: 24750
In all the tutorials or examples I find, they show a colon after the selector name (getCurrentData:
), like so:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(getCurrentData:)
name:@"mapsReceived"
object:nil ];
Since there is nothing after this colon in the examples, I assume that the method specified does not take any arguments. And mine doesn't either, but when I use the above syntax my app crashes and says unrecognized selector sent to instance
I remove the colon, and it works. So why do all examples show a colon?
Secondly, if I did use a selector that required an argument, how can I do that? If I do this:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(createButtons:@"ten grand";)
name:@"mapsReceived"
object:nil ];
It won't even compile; I get tons of errors until I remove the text string and the colon. Yet I can call this method, with the string, just fine when using it outside NSNotification. Is there a way to do what I want here?
Upvotes: 2
Views: 187
Reputation: 57169
If there is a colon at the end then it takes a parameter. You do not pass it anything in the @selector
call. Your first example is correct and your getCurrentData:
method should look like this.
-(void)getCurrentData:(NSNotification*)note
{
//Implementation
}
For each colon you see in a selector that is how parameters it takes, for example both @selector(aMethod:secondArg:thirdArg:)
and @selector(aMethod:::)
have different signatures but both would take 3 arguments.
Upvotes: 1
Reputation: 5389
You're trying ti add an observer and send a message in the same code, it doesn't work that way. You declare that THIS method is used to react to an event as in [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"behaviorName" object:nil];
Then it will be called when the event happens, and you can pass it a special object via the object parameter to work on. However, behind the colon is supposed to be passed a special event encapsulation object (NSNotification obviously), see the documentation, which you are supposed to design your myMethod: method for. If you omitted the object in your method code, then it makes sense trying to call it fails, since you're calling another method, in effect an overload :p
Upvotes: 0
Reputation: 1839
The answer to the first question is that in Objective C the colon is part of the function name. If you omit it you refer to a different function, one that doesn't take any parameters. However, notifications methods are always called with (and must therefore accept) one parameter: the Notification object:
Here is an excerpt of the NSNotificationCenter Class Reference:
"The method specified by notificationSelector must have one and only one argument (an instance of NSNotification)"
Upvotes: 2
Reputation: 7895
First, most all examples assume that you are passing the notification to the method. For example:
- (void)respondToNotification:(NSNotification *);
This would be represented as:
@selector(respondToNotification:)
If you removed this argument, it would be
@selector(respondToNotification)
As for the second item, you cannot specify arguments in that manner. You need to architect your methods in such a way that it receives the notification as the argument, and then you can inspect the notification and call another method to perform whatever action you need.
Upvotes: 3
Reputation: 1411
When the NSNotificationCenter
sends a notification (via the selector you specify - eg getCurrentData:
), it will pass an instance of NSNotification
, so your method must accept a single argument which is such an instance.
Upvotes: 2