Reputation: 998
I've read many articles to understand why it's necessary to use @selector()
to refer to a method, but I don't think that I'm satisfied. When we specify an action for a button, for example, we have to write:
[btn addTarget:self action:@selector(myMethod)];
Why not simply:
[btn addTarget:self action:myMethod];
Please explain the need and reason, and what happens without it.
Upvotes: 8
Views: 1557
Reputation: 13889
Your example can work, if myMethod is an instance of SELECTOR
[btn addTarget:self action:myMethod];
For string you can use
[btn addTarget:self action:NSSelectorFromString(@"myMethod")];
explained here https://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Selector.html
Here is an example of a generic actionLinker function How to programmatically setup a CallBacks for a UIButton?
- (void)setRunButton:(UIButton *)objectName mySelector:(NSString *)action myControlEvent:(UIControlEvents)controlEvent {
[objectName addTarget:self action:NSSelectorFromString(action) forControlEvents:controlEvent];
}
if you want to use your own example with myMethod as an instance of selector, following code is more applicable
- (void)setRunButton:(UIButton *)objectName mySelector:(SEL)action myControlEvent:(UIControlEvents)controlEvent {
[objectName addTarget:self action:action forControlEvents:controlEvent];
}
Upvotes: 0
Reputation: 162712
I have read many articles in order to understand the @selector keyword but I dstill don't quite understand its purpose. I just want to ask why we have @selector.
It all has to do with parsing the C language.
On its own, in an expression like [obj performSelector:someRandomSelector]'
the compiler treats someRandomSelector
bit as "expand whatever someRandomSelector
is -- evaluating expressions, dealing with #defines, laying down a symbol for later linking, etc... -- and whatever that expansion yields better be a SEL.
Thus, if you were to write [obj performSelector:action]'
the compiler would have no way to know the difference between action
as a variable containing a potentially volatile selector and action
being the actual name of a method on obj
.
@selector()
solves this by creating a syntactic addition to the language that always evaluates to a constant SEL result.
Historically, Objective-C was originally implemented as a straight up extension to the C preprocessor. All the various @...
prefixed additions made that implementation much easier in that basically anything prefixed by an @
was an Objective-Cism.
Upvotes: 8
Reputation: 47609
It's a question of language design. You need something to say 'this is a selector' and that is the syntax they used to separate the text in the source file that describes the selector you are talking about and the code around it. This requires some kind of quote or bracket round it. @selector(...)
is just the syntax they went with.
What if you had a selector that contains :
, for example called thingWithX:y:z:
? You couln't have [btn addTarget:self action:thingWithX:y:z:]
as the colons would confuse the compiler. You would have to have [btn addTarget:self action:@selector(thingWithX:y:z:)]
so it can separate the selector of the btn action:
and the selector it references `thingWithX:y:z:
.
Upvotes: 1
Reputation: 10994
Here is the docs on selectors:
I think you will understand after reading it.
Upvotes: 0