美文网首页
iOS SEL的本质

iOS SEL的本质

作者: huisedediao | 来源:发表于2021-02-03 14:23 被阅读0次
结论: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

相关文章

网友评论

      本文标题:iOS SEL的本质

      本文链接:https://www.haomeiwen.com/subject/agoptltx.html