5

I was wondering: what's the difference between writing a selector name with no colon @selector(mySelector), or @selector(mySelector:) with the colon?

As in:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWith... 
                                                       target:self
                                                       action:@selector(addAction:)];

I can't find another example without the colon, but I'm quite sure I have already seen some of them.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Paul
  • 6,108
  • 14
  • 72
  • 128
  • 2
    Colon means there is a parameter, without colon means there is no parameter after this piece. – Maurício Linhares Sep 05 '11 at 16:05
  • 1
    possible duplicate of [Objective-C performSelector -- when to use colon](http://stackoverflow.com/questions/1546372/objective-c-performselector-when-to-use-colon); also [When to use a colon with a selector](http://stackoverflow.com/questions/4953623/when-to-use-a-colon-with-a-selector) – jscs Sep 05 '11 at 16:54

2 Answers2

14

The colon is needed after the method's name if and only if the method takes an argument.

No function parameters:

-(void)addAction {}

// Use ...@selector(addAction)...

Has parameter:

-(void)addAction:(id)info {}

// Use ...@selector(addAction:)...
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

In certain cases, the number of colons can determine arguments. For example, if you pass in an action method with one colon, it'll send the sender as the first argument. If you pass in a selector with two colons, you'll get the event as well. No colon means, obviously, no arguments.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
  • In which cases the number of colons doesn’t determine the number of arguments? –  Sep 05 '11 at 16:28
  • I know it does in the case of target-action, but I'm thinking that the plain 'performSelector' method (not the ones that take arguments) might have an issue with that—at best it'd pass in nil arguments. – FeifanZ Sep 05 '11 at 16:45