美文网首页
Runtime-类与对象

Runtime-类与对象

作者: 刘刘浩 | 来源:发表于2020-05-31 09:53 被阅读0次

类与对象的关系

这张图想必大家不会陌生,它展示了对象、类、元类之间的关系


类与对象
  • 对象(Instance)的isa指针指向类(Class)
  • 类(Class)的isa指针指向元类(Meta Class)

对象

对象在objc.h中的数据结构为objc_object

  • 它仅包含一个指向类(Class)的isa指针
  • objc_object *即为id类型,可以指向任何OC对象
typedef struct objc_class *Class;
typedef struct objc_object *id;
/// Represents an instance of a class.
struct objc_object {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
};

而objc_object在objc-private.h中的定义,isa的数据结构换成了isa_t

struct objc_object {
private:
    isa_t isa;
...
}

isa_t是一个共用体(所有成员占用同一段内存,修改其中一个会影响其他成员)

union isa_t {
    isa_t() { }
    isa_t(uintptr_t value) : bits(value) { }

    Class cls;
    uintptr_t bits; // typedef unsigned long   uintptr_t;
#if defined(ISA_BITFIELD)
    struct {
        // isa 结构的定义
        ISA_BITFIELD;  // defined in isa.h
    };
#endif
};

在arm64架构下,ISA_BITFIELD定义如下

#   define ISA_BITFIELD                                                      \
      uintptr_t nonpointer        : 1;                                       \
      uintptr_t has_assoc         : 1;                                       \
      uintptr_t has_cxx_dtor      : 1;                                       \
      uintptr_t shiftcls          : 33; /*MACH_VM_MAX_ADDRESS 0x1000000000*/ \
      uintptr_t magic             : 6;                                       \
      uintptr_t weakly_referenced : 1;                                       \
      uintptr_t deallocating      : 1;                                       \
      uintptr_t has_sidetable_rc  : 1;                                       \
      uintptr_t extra_rc          : 19

从bits和ISA_BITFIELD的定义不难看出,isa_t占用64位,仅使用shiftcls所占用的33位来储存Class、Meta-Class对象的内存地址信息

类在runtime.h中的定义为objc_class

  • 它也包含一个指向类(这里实际会指向元类)的isa指针
  • 另外还包含了一些数据结构来储存变量、方法等
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;

而在objc_class的私有定义(objc-runtime-new.h)中可以看到更多的信息

  • 它继承自objc_object,所以也是对象
  • OC中的Class类型即为objc_class *类型
  • 变量、方法等信息都储存在class_data_bits_t结构中
typedef struct objc_class *Class;
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
    ...
};

相关文章

  • Runtime-原理

    runtime初探对象与方法的本质runtime-消息发送runtime-动态方法解析runtime-消息转发 r...

  • Runtime-类与对象

    类与对象的关系 这张图想必大家不会陌生,它展示了对象、类、元类之间的关系 对象(Instance)的isa指针指向...

  • Runtime-(二)对象,类对象,元类对象

    类对象存储实例方法列表等信息 元类对象存储类方法列表等信息 先献祭出一副官方的图 这幅图相信大多数做iOS的都不会...

  • Runtime-对象,类对象和元类对象

    类对象存储实例方法列表等信息,实例对象可以通过isa指针找到自己的类对象,来访问实例方法元类对象存储类方法列表等信...

  • Runtime-类

    这里会把类相关、程序启动类信息填充、引用计数都会讲下。想要深入了解OC的动态性,就必须去研究runtime的代码,...

  • runtime-关联对象

    前言 场景:需要用一个系统的类,但系统的类并不能满足需求。你需要额外添加一个属性。一般解决办法就是继承。但只增加一...

  • 类与对象(类与对象定义)

    类与对象的定义和使用 如果在程序之中要定义一个类可以使用class关键字完成,而定义的语法如下: 在这个类中只是定...

  • runtime-整理中

    runtime-整理中

  • Swift Runtime-引用计数

    前言 在Swift Runtime-初探一文里,我们初步研究了对象的内存结构.有metadata及Refcount...

  • 函数类和对象区别

    类与类:行为不同 对象与对象:数据不同

网友评论

      本文标题:Runtime-类与对象

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