Use colon : or not with selectors

大城市里の小女人 提交于 2019-12-07 10:50:35

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7310392/use-colon-or-not-with-selectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!