6

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.

Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
  • 1
    This is a more detailed answer... http://stackoverflow.com/a/19322677/1016102 – regetskcob Feb 18 '15 at 10:44
  • @DanielBocksteger wow thank you so much man! I have no idea why i didn't find this answer before. Please post this link to answer, I want to choose your answer as correct. It really saved my time! – Alexander Perechnev Feb 18 '15 at 10:47
  • 1
    SEL is closed structure. It content shouldn't bother you. – Cy-4AH Feb 18 '15 at 10:53
  • This is the answer http://stackoverflow.com/a/19322677/1016102 which Alexander searched for... but dehlens is correct and good too :-) – regetskcob Feb 18 '15 at 11:01
  • `objc_selector` has no implementation, cause the c/c++ compiler allow undefined struct, plz refer `https://gist.github.com/Jichao/f663400a0a56011171291bbac9aa372b` – Jichao Oct 01 '20 at 08:29

1 Answers1

4

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

dehlen
  • 7,325
  • 4
  • 43
  • 71
  • Also you should not assume that the implementation stays the same. Only because it's a C string right now doesn't mean it's one in the future. – Micky Feb 18 '15 at 10:50
  • You shouldn't assume that the implementation is the same on 32 and 64 bit MacOS X, on different versions of MacOS X, and on 32 and 64 bit iOS, including again different versions of iOS. Just because it's a C string right now in one of several implementations of Objective-C doesn't mean it's the same on all. – gnasher729 Feb 18 '15 at 11:29
  • Unfortunately the blog link is now dead, "Sorry, the blog at unixjunkie.blogspot.com has been removed. This address is not available for new blogs." – jrh Jan 23 '19 at 00:32