Selector Literals (Oxygene for Cocoa)

The selector keyword can be used to get a selector reference on the Cocoa platform. Selectors are feature unique to the Cocoa platform and Object Model, and they represent the name of an Objective-C runtime method.

Selectors are held in a type called SEL, and can be used to dynamically invoke methods, for KVO, Notification center, or to pass them to other Cocoa APIs that expect a aSEL type.

A selector is declared with the selector keyword, followed by parenthesis enclosing the Objective-C name of the method, which is made up of the name, plus a colon if the method takes parameters, plus additional name and colon pairs for all parts of a multi-part method:

method compare(aOther: id)_ options(aOptions: NSCompareOptions);
var s := selector(compare:options:);

Using the selector literal syntax will cause the compiler to perform checks if the specified selector is valid and known, and a warning will be emitted if a selector name is provided that does not match any method known to the compiler. This provides extra safety over using the NSSelectorFromString function.

Note that by default, the Elements compiler might mangle type names and method names, for example to ensure uniqueness of overloaded methods. You can specify the [objc] Aspect on a method to prevent mangling and make its internal name on the Objective-C runtime match the selector name.

Cocoa Only

The selector keyword is relevant and available on the Cocoa platform only, and selectors can be used only to refer to methods on classes that use the Cocoa Object Model.

See Also