探究Class本質(zhì)

比較簡(jiǎn)單,只是記錄一下知識(shí)點(diǎn)。

一、 Class
1.網(wǎng)上扣了一張圖片:


image.png

Class 的一個(gè)結(jié)構(gòu)。
關(guān)于為什么&FAST_DATA_MASK是一種計(jì)算位域方法,取出對(duì)應(yīng)位域的值,具體可以看之前寫(xiě)的一篇探究 isa 本質(zhì)。

2.class_rw_t 真實(shí)定義:

class_rw_t:讀寫(xiě)、運(yùn)行時(shí)調(diào)用方法都是取之于此、類(lèi)的初始內(nèi)容、分類(lèi)的內(nèi)容。

struct class_rw_t {
    // Be warned that Symbolication knows the layout of this structure.
    uint32_t flags;
    uint32_t version;
    const class_ro_t *ro;
    method_array_t methods;
    property_array_t properties;
    protocol_array_t protocols;
    Class firstSubclass;
    Class nextSiblingClass;
};
class method_array_t : 
    public list_array_tt<method_t, method_list_t> 
{
    typedef list_array_tt<method_t, method_list_t> Super;

 public:
    method_list_t **beginCategoryMethodLists() {
        return beginLists();
    }
    
    method_list_t **endCategoryMethodLists(Class cls);

    method_array_t duplicate() {
        return Super::duplicate<method_array_t>();
    }
};

method_array_t->method_list_t->method_t

3.class_ro_t 真實(shí)定義:

class_ro_t:只讀、包含的是類(lèi)的初始內(nèi)容。

struct class_ro_t {
    uint32_t flags;
    uint32_t instanceStart;
    uint32_t instanceSize;
#ifdef __LP64__
    uint32_t reserved;
#endif

    const uint8_t * ivarLayout;
    
    const char * name;
    method_list_t * baseMethodList;
    protocol_list_t * baseProtocols;
    const ivar_list_t * ivars;

    const uint8_t * weakIvarLayout;
    property_list_t *baseProperties;

    method_list_t *baseMethods() const {
        return baseMethodList;
    }
};

method_list_t -> method_t

4.method_t 真實(shí)定義:
struct method_t {
SEL name;
const char *types;
IMP imp;
};

SEL name:方法名字,方法選擇器
const char *types:方法類(lèi)型,字符串表示,方法編碼
IMP imp:函數(shù)指針,函數(shù)實(shí)現(xiàn)地址

5.Type Encoding
iOS中提供了一種@encode的指令,可以將具體類(lèi)型用字符串表示出來(lái)。


image.png
NSLog(@"%s",@encode(id));

2018-09-03 11:51:37.186767+0800 runtime[1030:38253] @
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容