Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the @selector directive for? Why not simply use the name of the method?

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.

like image 668
Rais Iqbal Avatar asked May 25 '12 16:05

Rais Iqbal


People also ask

What is the defaultvalueaccessor directive?

The example above is the DefaultValueAccessor directive extracted from the forms module. The selector combines all of the various selectors (element, attribute), chaining selectors, exclusion selectors and combining these selectors to target various HTML elements.

Where should a directive be placed in a source code file?

At the beginning of a source code file, before any namespace or type declarations. In any namespace, but before any namespaces or types declared in that namespace, unless the global modifier is used, in which case the directive must appear before all namespace and type declarations. Otherwise, compiler error CS1529 is generated.

What is the use of using directive?

The using directive allows you to use types defined in a namespace without specifying the fully qualified namespace of that type. In its basic form, the using directive imports all the types from a single namespace, as shown in the following example:

What is the difference between element selector and ID selector?

The element selector selects HTML elements based on the element name. The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element!


1 Answers

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.

like image 73
bbum Avatar answered Sep 28 '22 04:09

bbum