• 实例对象( instance)、类对象(class)、元类对象(meta-class)的内部结构分析


    实例对象( instance)、类对象(class)、元类对象(meta-class)的内部结构分析

    本文使用的objc源码版本为objc4-756.2

    Class本质上为一个结构体类型

    typedef struct objc_class *Class;

    实例对象的定义

    struct objc_object {
    private:
        isa_t isa;    
        //以下省略是其他继承添加成员变量
    }

    类对象/元类对象的定义

    struct objc_class : objc_object {
        // Class ISA;
        Class superclass;
        cache_t cache;             // 方法缓存
        class_data_bits_t bits;    // 类的具体信息
        class_rw_t *data() { 
            return bits.data();
    }
        //以下省略
    }
    
    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;    //协议列表
    }
    
    struct class_ro_t {
        uint32_t flags;
        uint32_t instanceStart;
        uint32_t instanceSize;    //instance对象占用的内存大小
    #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;
    }

    Xcode 里面的runtime里面的 API是已经废弃了如下

    /// An opaque type that represents an Objective-C class.
    typedef struct objc_class *Class;
    
    /// Represents an instance of a class.
    struct objc_object {
        Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
    };

    废弃如下  OBJC2_UNAVAILABLE; 

    struct objc_class {
        Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
    
    #if !__OBJC2__
        Class _Nullable super_class                              OBJC2_UNAVAILABLE;
        const char * _Nonnull name                               OBJC2_UNAVAILABLE;
        long version                                             OBJC2_UNAVAILABLE;
        long info                                                OBJC2_UNAVAILABLE;
        long instance_size                                       OBJC2_UNAVAILABLE;
        struct objc_ivar_list * _Nullable ivars                  OBJC2_UNAVAILABLE;
        struct objc_method_list * _Nullable * _Nullable methodLists                    OBJC2_UNAVAILABLE;
        struct objc_cache * _Nonnull cache                       OBJC2_UNAVAILABLE;
        struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
    #endif
    
    } OBJC2_UNAVAILABLE;
  • 相关阅读:
    用图片来代替字符串
    下载网页时的 有gzip压缩的处理
    位置不固定验证码的识别
    CookieContainer 与 Session
    Thread Pool 备忘
    用 SGMLReader把子HTML 转 XML
    非asp.net控件实现回发 button
    ajax.net ??= 回车
    如何写需求分析
    jsp中地址
  • 原文地址:https://www.cnblogs.com/junhuawang/p/13573262.html
Copyright © 2020-2023  润新知