How do SEL and @selector work?

typedef struct objc_selector  *SEL;

In the above code, SELenter objective-c pointer to struct objc_selector. So, if I make a variable SEL, for example:

SEL aSel = @selector(instanceMethod) //Like this

What's happening? What did @selectorthe instance method instanceMethoddo?

+4
source share
2 answers

The directive @selectorsimply takes the name of the method and returns the corresponding identifier for this method. This identifier is used to determine which method to call when the selector is ultimately executed:

SEL aSel = @selector(instanceMethod);

// Calls -instanceMethod on someObject
[someObject performSelector:aSel];

See your Apple documentation for details .

+7
source

SEL const char[], . , C:

(lldb) p _cmd
(SEL) $0 = "windowDidLoad"
(lldb) p (const char*) _cmd
(const char *) $1 = 0x91ae954e "windowDidLoad"

, , , ==. C, ("instanceMethod" == @selector(instanceMethod) ), : , , SEL .

@selector(instanceMethod) C "instanceMethod", Obj-C, , .

SEL sel = @selector(instanceMethod);
SEL sel = sel_registerName("instanceMethod");

P.S. struct objc_selector , SEL C . Obj-C , sel_getName sel_registerName.

+18

All Articles