Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the objc_selector implementation? [duplicate]

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.

like image 766
Alexander Perechnev Avatar asked Feb 18 '15 10:02

Alexander Perechnev


People also ask

What are selectors in Objective-C?

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.

Is there an objc_selector in Objective C?

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.

What is a selector in iOS?

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.

What is @selector(instancemethod) syntax in Objective-C?

@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");


1 Answers

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 struct objc_selector, but what the heck is an objc_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 simple C strings. For example, if we define a Foo class with a - (int)blah method, the code NSLog(@"SEL = %s", @selector(blah)); would output SEL = blah.

Taken from: here

like image 152
dehlen Avatar answered Oct 29 '22 18:10

dehlen