类对象本质为objc_class结构体。类对象⾥⾯存储了类的⽗类、属性、实例⽅法、协议、成员变量、⽅法缓存等等
struct objc_class : objc_object {
// Class ISA; // 8 objc_object 结构体属性
Class superclass;// 8
cache_t cache; // 16
class_data_bits_t bits;
...
}
获取实例对象的类对象 runtime API
Class class1 = [Person class];
Class class2 = [Person alloc].class;
Class class3 = object_getClass([Person alloc]);
isa 探索
void TestNSObject(void){
// NSObject实例对象
NSObject *object1 = [NSObject alloc];
// NSObject类对象
Class class =object_getClass(object1);
// NSObject元类(根元类)
Class metaClass = object_getClass(class);
NSLog(@"NSObject实例对象:%p",object1);
NSLog(@"NSObject类对象:%p",class);
NSLog(@"NSObject元类(根元类):%p",metaClass);
// Person -- 元类的父类就是父类的元类
Class pMetaClass = objc_getMetaClass("Person");
Class psuperClass =class_getSuperclass(pMetaClass);
NSLog(@"%@ - %p",pMetaClass,pMetaClass);
NSLog(@"%@ - %p",psuperClass,psuperClass);
// Teacher继承自Person
// Teacher元类的父类 就是 Person(Person的元类)
Class tMetaClass =objc_getMetaClass("Teacher");
Class tsuperClass =class_getSuperclass(tMetaClass);
NSLog(@"%@ - %p",tsuperClass,tsuperClass);
// NSObject的父类
Class nsuperClass = class_getSuperclass(NSObject.class);
NSLog(@"%@ - %p",nsuperClass,nsuperClass);
// 根元类的父类 -- NSObject
Class rnsuperClass =class_getSuperclass(metaClass);
NSLog(@"%@ - %p",rnsuperClass,rnsuperClass);
}
实例对象: isa + 成员变量的值
isa 是一个nonpointerIsa,其中几位是类对象的内存地址,所以
isa 走向 实例对象 isa -->类对象 isa -->元类对象 isa -->根元类 isa -->根元类自己 --
元类的继承关系
⽗类的元类就是元类的⽗类。根元类的⽗类就是NSObject。NSObject是万类之祖。
class_data_bits_t 存储方法,属性,成员变量
成员变量存放在类对象的class_ro_t结构体当中
lldb调试
(lldb) x/6gx p.class
0x100008250: 0x0000000100008228 0x000000010080e140
0x100008260: 0x0000000100d6a430 0x0002802c00000003
0x100008270: 0x0000000100b48cc4 0x0000000000000000
(lldb) p/x (class_data_bits_t *)0x100008270
(class_data_bits_t *) $1 = 0x0000000100008270
(lldb) p $1->data()
(class_rw_t *) $2 = 0x0000000100b48cc0
(lldb) p *$2
(class_rw_t) $3 = {
flags = 2148007936
witness = 1
ro_or_rw_ext = {
std::__1::atomic = {
Value = 4295000512
}
}
firstSubclass = nil
nextSiblingClass = 0x00007ff8592549b8
}
(lldb) p $3.methods()
(const method_array_t) $4 = {
list_array_tt = {
= {
list = {
ptr = 0x0000000100008098
}
arrayAndFlag = 4295000216
}
}
}
(lldb) p $4.list
(const method_list_t_authed_ptr<method_list_t>) $5 = {
ptr = 0x0000000100008098
}
(lldb) p $5.ptr
(method_list_t *const) $6 = 0x0000000100008098
(lldb) p *$6
(method_list_t) $7 = {
entsize_list_tt = (entsizeAndFlags = 27, count = 6)
}
(lldb) p $7.get(0)
(method_t) $8 = {}
(lldb) p $8.getDescription()
(objc_method_description *) $9 = 0x00000001000080a0
(lldb) p *$9
(objc_method_description) $10 = (name = "instanceMethod", types = "v16@0:8")
(lldb) p $7.get(5).getDescription()
(objc_method_description *) $11 = 0x0000000100008118
(lldb) p *$11
(objc_method_description) $12 = (name = "setAge:", types = "v20@0:8i16")
(lldb) p $7.get(4).getDescription()
(objc_method_description *) $13 = 0x0000000100008100
(lldb) p *$13
(objc_method_description) $14 = (name = "age", types = "i16@0:8")
(lldb) p *($7.get(3).getDescription())
(objc_method_description) $15 = (name = "setName:", types = "v24@0:8@16")
(lldb) p *($7.get(2).getDescription())
(objc_method_description) $16 = (name = ".cxx_destruct", types = "v16@0:8")
.cxx_destruct⽅法是在ARC模式下⽤于释放成员变量的。只有当前类拥有实例变量时这个⽅法才会出现,property⽣成的实例变量也算,且⽗类的实例变量不会导致⼦类拥有这个⽅法。
苹果为什么设计元类?
能够复⽤消息传递这套机制。不管你是什么类型的⽅法,都是同⼀套流程
ro,rw,rwe区别
ro 编译时生成,存类的属性,实例⽅法,协议,不可修改
class_ro_t是在编译的时候⽣成的。当类在编译的时候,类的属性,实例⽅法,协议这些内容就存在class_ro_t这个结构体⾥⾯了,这是⼀块纯净的内存空间,不允许被修改
struct class_rw_t {
...
explicit_atomic<uintptr_t> ro_or_rw_ext
...
private:
using ro_or_rw_ext_t = objc::PointerUnion<const class_ro_t, class_rw_ext_t, PTRAUTH_STR("class_ro_t"), PTRAUTH_STR("class_rw_ext_t")>;
...
}
rw 结构体能访问到 ro和 rwe
class_rw_t是在运⾏的时候⽣成的,类⼀经使⽤就会变成class_rw_t,它会先将class_ro_t的内容"拿"过去,然后再将当前类的分类的这些属性、⽅法等拷⻉到class_rw_t⾥⾯。它是可读写的。
const method_array_t methods() const {
auto v =get_ro_or_rwe();
if(v.is<class_rw_ext_t *>()) {
return v.get(&ro_or_rw_ext)->methods;
}else{
return method_array_t{v.get<const class_ro_t*>(&ro_or_rw_ext)->baseMethods};
}
}
获取方法时,从class_rw_ext_t 取 或 从class_ro_t取
class_rw_ext_t可以减少内存的消耗。苹果在wwdc2020⾥⾯说过,只有⼤约10%左右的类需要动态修改。所以只有10%左右的类⾥⾯需要⽣成class_rw_ext_t这个结构体。这样的话,可以节约很⼤⼀部分内存。
class_rw_ext_t⽣成的条件:
⽤过runtime的Api进⾏动态修改的时候。或有分类的时候,且分类和本类都为⾮懒加载类(实现了+load⽅法)的时候。
cache_t 缓存
⽅法的缓存基于不同架构,缓存策略是不⼀样的。以下是⽅法缓存的核⼼代码的部分截图,这⾥就体现了在不同架构下的不同的缓存策略。