When to use a colon with a @selector

前端 未结 6 1209
傲寒
傲寒 2020-12-03 04:10

Just getting going with iPhone development and Objective-C.

Yesterday I was trying to addObserver for a notification in a view of mine, and I kept getti

相关标签:
6条回答
  • 2020-12-03 04:15

    You seem to be missing one concept here: colon is, in some way, a part of the method name. E.g., method

    -(IBAction) doIt:(id)sender;
    

    has name doIt:. Thus, colon should be used to reference this method.
    But this method doesn't have a colon at the end

    -(IBAction) doItWithoutParameter;
    

    Same goes for methods accepting multiple arguments, they have names like doItWithParam1:andParam2:

    0 讨论(0)
  • 2020-12-03 04:22

    In your case:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nameOfMySelector:) name:@"BBLocationServicesAreDisabled" object:nil];
    
    - (void) nameOfMySelector: (NSNotification *) notification {
        /* this method would require the semi-colon */
    }
    

    or in this case:

    [self.callToActionButton addTarget:self action:@selector(nameOfMySelector:) forControlEvents:UIControlEventTouchUpInside];
    
    - (void) nameOfMySelector: (id) sender {
        /* this method would also require the semi-colon */
    }
    
    0 讨论(0)
  • 2020-12-03 04:23

    The colon indicates that the method takes a parameter.

    [someObject performSelector:@selector(doSomething:)] means that doSomething is expecting a parameter.

    [someObject performSelector:@selector(doSomething)] means that doSomething doesn't need any parameters.

    0 讨论(0)
  • 2020-12-03 04:29

    A selector represents a method name, and the number of colons in a selector matches the number of arguments in the corresponding method:

    1. mySelector — no colon, no arguments, e.g. - (void)mySelector;, [self mySelector];
    2. mySelectorWithFoo: — one colon, a single argument, e.g. - (void)mySelectorWithFoo:(Foo *)foo;, [self mySelectorWithFoo:someFoo];
    3. mySelectorWithFoo:withBar: — two colons, two arguments, e.g. - (void)mySelectorWithFoo:(Foo *)foo bar:(Bar *)bar;, [self mySelectorWithFoo:someFoo bar:someBar];

    and so forth.

    It is also possible to have a selector without ‘naming’ the parameters. It’s not recommended since it’s not immediately clear what the parameters are:

    1. mySelector:: — two colons, two arguments, e.g. - (void)mySelector:(Foo *)foo :(Bar *)bar;, [self mySelector:someFoo :someBar];
    2. mySelector::: — three colons, three arguments, e.g. - (void)mySelector:(int)x :(int)y :(int)z;, [self mySelector:2 :3 :5];
    0 讨论(0)
  • 2020-12-03 04:30

    I think the problem is the missing parameter.

    See this post: Objective-C: Calling selectors with multiple arguments (Great answers!)

    0 讨论(0)
  • 2020-12-03 04:35

    As mentioned by boltClock, the character you are referring to is actually a colon. The difference between @selector(method) and @selector(method:) is the method signature. The 2nd variant expects a parameter to be passed.

    @selector(method) would expect the method: -(void)method

    @selector(method:) would expect the method: -(void)method:(id)someParameter

    0 讨论(0)
提交回复
热议问题