美文网首页
类初始化过程

类初始化过程

作者: 事件_666 | 来源:发表于2019-07-05 10:24 被阅读0次

    struct objc_class : objc_object {
    // Class ISA;
    Class superclass;
    cache_t cache; // formerly cache pointer and vtable
    class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags

    class_rw_t *data() { 
        return bits.data();//重点在这里
    }
    

    ----------第一层-----------------------
    class_rw_t* data() {
    return (class_rw_t *)(bits & FAST_DATA_MASK);
    }
    ------------第二层---------------------
    //1.表示是否swift

    define FAST_IS_SWIFT (1UL<<0)

    //2.当前类或者父类是否有retain、release等方法

    define FAST_HAS_DEFAULT_RR (1UL<<1)

    // class's instances requires raw isa

    //3.类是否需要初始化isa

    define FAST_REQUIRES_RAW_ISA (1UL<<2)

    // data pointer

    //4.数据段指针 1111111111 1111111111 1111111111 1111111111 1111000

    define FAST_DATA_MASK 0x00007ffffffffff8UL

    ----第三层 到此找到 rw-------
    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;///RO很重要
    
    method_array_t methods;
    property_array_t properties;
    protocol_array_t protocols;
    
    Class firstSubclass;
    Class nextSiblingClass;
    
    char *demangledName;
    

    -----------------到此已经找到了类的所有结构体可以对类初始化---------
    //1.把ro中的方法添加到rw中
    method_list_t *list = ro->baseMethods();
    if (list) {
    prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls));
    rw->methods.attachLists(&list, 1);
    }

    //2.把ro中的属性添加到rw中
    property_list_t *proplist = ro->baseProperties;
    if (proplist) {
        rw->properties.attachLists(&proplist, 1);
    }
    
    //3.把ro中的协议添加到rw中
    protocol_list_t *protolist = ro->baseProtocols;
    if (protolist) {
        rw->protocols.attachLists(&protolist, 1);
    }
    
    //把分类中的方法、属性、协议添加到类中去
    category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
    attachCategories(cls, cats, false /*don't flush caches*/);
    

    ------------------到此完成了一个类的初始化------------------

    相关文章

      网友评论

          本文标题:类初始化过程

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