objc_selector 实现是什么?

What is the objc_selector implementation?

我发现 SEL 类型有下一个定义:

typedef struct objc_selector *SEL;

但我找不到 objc_selector 是如何实现的。

好的,如果我们有下一个代码

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
  SEL mySelector = NSSelectorFromString(@"mySelector");
  return 0;
}

,那么mySelector只是一个指针。在它包含的地址之后,我们看到了 C 字符串,可以 表示为:

const char* mySelector = "mySelector";

但是 objc_selector 不是 C 字符串,它是结构并且可以包含其他内容。所以我想知道objc_selector结构是如何实现的。

这可能对您有帮助:

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.

取自:here