I have been reading Shane’s book and other material as well as practicing on my own, Objective-c methods. One term that I come across is “Selector”. I know some of its usages in cases such as sorting, and calling other handlers. Could you please compile a list if possible with methods and short examples of their usages. The NSObject class has several of them.
Any method, your own or a cocoa one can be a selector. It’s just a way to pass a method as a parameter to certain methods. In ASOC, you just put the method name in parentheses, but you use colons like in cocoa instead of underscores, one for each parameter. So if you had a method of your own called doSomething_, you could pass that to a method like performSelector_withObject_afterDelay_ like this:
If a method doesn’t take any parameters, then you just use the name in quotes. Usually, the method with selector in its name defines what kind of selector you can pass as the argument, especially with respect to the number of arguments that selector can take. Like performSelector_withObject_afterDelay_, can only use a selector with one argument because you can only pass one argument (in the withObject parameter) to it. NSObject has another method performSelector_withObject_withObject_, that can use a selector that take two arguments.
Could you be more specific? A selector is (only for Objective-C) the identifier/key to a method. So technically every method of every object you create or found in any objective-c framework is a selector.
Maybe this clarifies it a bit more, if you have a handler like this (For now I will name the handler identifiers selectors)
Void would be the selector, or even better said «handler void» is the selector. This can be set into a variable and passed around your script to and from other objects, like a selector can in Objective-C. Here is also the advantage of Objective-C’s selectors compared with AppleScript’s is that a selector is like objectValue:forKey:. Because the selectors are much more detailed you can work with optional parameters (which lacks in AppleScript and therefore the underscore separation in method names).
I stay in AppleScript because in Objective-C would maybe a bit to difficult to understand I think. I hope with comments in the script I will clarify what selectors are…
--define a handler and give it a selector named "void"
--x is a dynamic selector (created at run time)
set x to void
--to call the script defined selector; note that selectors in AppleScript are preserved like properties between each runs. This means that the second run the error will occur here already. Compile again to intialize the handler void.
--to call the dynamic selector
--clear selector void by setting another value; set to missing value
set void to missing value
--try to call void selector again
void() --throws an error because selector void won't point to a handler anymore but is missing value