iOS SEL的本质

2021-02-03  本文已影响0人  huisedediao
结论:SEL是指向字符串的指针。



源码中对SEL的定义:

/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;

可以看出SEL是指向struct objc_selector类型的指针



把OC转成C++,截取部分代码,代码如下:

struct _objc_method {
    struct objc_selector * _cmd;
    const char *method_type;
    void  *_imp;
};

static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_method method_list[2];
} _OBJC_PROTOCOL_INSTANCE_METHODS_NSCoding __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    2,
    {{(struct objc_selector *)"encodeWithCoder:", "v24@0:8@16", 0},
    {(struct objc_selector *)"initWithCoder:", "@24@0:8@16", 0}}
};

可以看到:struct objc_selector *类型的参数,是通过把char *强转成struct objc_selector *而来的。从SEL的定义可以看出SEL就等价于struct objc_selector *,到此基本可以断定SEL就是指向字符串的指针。

以打印c字符串内容的方式打印SEL:

image.png
上一篇 下一篇

猜你喜欢

热点阅读