开发者

Use colon : or not with selectors

开发者 https://www.devze.com 2023-04-03 06:55 出处:网络
I was wondering: what\'s the difference between writing a selector name with no colon @selector(mySelector), or @selector(mySelector:) with the colon?

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.开发者_JAVA技巧.. 
                                                       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.


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:)...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消