I've found that the SEL
type has the next definition:
typedef struct objc_selector *SEL;
But I can't find how is objc_selector
implemented.
Okay, if we have the next code
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
SEL mySelector = NSSelectorFromString(@"mySelector");
return 0;
}
, then mySelector
is just a pointer. Following the address which it contains, we see the C-string, which can be presented like:
const char* mySelector = "mySelector";
But objc_selector
is not a C-string, it is structure and it can contain something else. So I want to know how objc_selector
structure is implemented.
The Objective-C language is based upon selectors. A selector is a message that can be sent to an object or a class. Xamarin.iOS maps instance selectors to instance methods, and class selectors to static methods.
P.S. struct objc_selector does not exist, it is to make SEL values incompatible with C strings, and hide selector implementation details from you. For even better understanding, you may read in Obj-C runtime source code for selectors what those sel_getName and sel_registerName actually do.
A selector is a message that can be sent to an object or a class. Xamarin.iOS maps instance selectors to instance methods, and class selectors to static methods.
@selector (instanceMethod) syntax creates C string "instanceMethod" then passes to Obj-C runtime function which turns it into unique pointer value corresponding to that string. What it basically does is SEL sel = @selector (instanceMethod); SEL sel = sel_registerName ("instanceMethod");
This might help you:
Now this one is fun and interesting. SEL is the type of a "selector" which identifies the name of a method (not the implementation). So, for example, the methods
-[Foo count]
and-[Bar count]
both share a selector, namely the selector"count"
. A SEL is a pointer to a structobjc_selector
, but what the heck is anobjc_selector
? Well, it's defined differently depending on if you're using the GNU Objective-C runtime, or the NeXT Objective-C Runtime (like Mac OS X). Well, it ends up that Mac OS X maps SELs to simpleC strings
. For example, if we define a Foo class with a- (int)blah method
, the codeNSLog(@"SEL = %s", @selector(blah));
would outputSEL = blah.
Taken from: here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With