第五篇:IOS类探究(成员变量值放在哪里?,成员变量信息放在哪里?)

我们简单写个demo,在我们定义的类HPWPerson里放了name,age属性,还有_hobby成员变量

@interface HPWPerson : NSObject {
    NSString *_hobby;
}

@property (nonatomic ,copy) NSString *name;
@property (nonatomic ,assign) int age;

第一我们思考两个问题,类方法是放在哪里?成员变量是放在哪里?带着这两个问题我们进行深入的探究下。

我们通过上篇结尾的分析实则知道,实例方法,成员属性,协议等都是存放在class_rw_t这个结构体里,如下面源码所示,

struct class_rw_t {
    // Be warned that Symbolication knows the layout of this structure.
    uint32_t flags;
    uint16_t witness;
#if SUPPORT_INDEXED_ISA
    uint16_t index;
#endif

    explicit_atomic<uintptr_t> ro_or_rw_ext;

    Class firstSubclass;
    Class nextSiblingClass;

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")>;

    const ro_or_rw_ext_t get_ro_or_rwe() const {
        return ro_or_rw_ext_t{ro_or_rw_ext};
    }

    void set_ro_or_rwe(const class_ro_t *ro) {
        ro_or_rw_ext_t{ro, &ro_or_rw_ext}.storeAt(ro_or_rw_ext, memory_order_relaxed);
    }

我们继续在class_rw_t结构体源码里找下,发现有class_ro_t这个结构体,这个结构体是干什么的呢?

  const class_ro_t *ro() const {
        auto v = get_ro_or_rwe();
        if (slowpath(v.is<class_rw_ext_t *>())) {
            return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->ro;
        }
        return v.get<const class_ro_t *>(&ro_or_rw_ext);
    }

我们通过打印得到如下:

第五篇:IOS类探究(成员变量值放在哪里?,成员变量信息放在哪里?)

@property (nonatomic ,copy) NSString *name;这个会生成下划线的成员变量_name,
@property (nonatomic ,assign) int age;这个会生成下划线的成员变量_age,
发现我们再打印class_ro_t里发现有上图所示的成员变量,所以其中“_hobby”,“_age”,”_name”这些是存在class_ro_t这个结构里的。

通过上面我们发现,成员变量的值是放在对象里,成名变量名字以及一些大小信息放在类里面,这个是为什么呢?实则类里面的结构体它就好比一个模板,通过这个模板就可以生成各个成员变量信息,但是成员变量的值是不同的所以成员变量的值要存放在实例对象里,成员变量名及大小信息放在类里面就可以。

接着我们再继续探究下,在class_rw_t这个结构体里有class_ro_t这个结构体,那这两个结构体有什么关系呢?

1.class_ro_t是在编译的时候生成的(只读),是一个纯净的空间,不能被修改的

  1. class_rw_t是运行时候生成的会把class_ro_t里整个剪切放到class_rw_t里

我们知道苹果的runtime可以动态的修改属性和方法,但是ro里又不支持修改的,那它是如何实现的呢?
我们先看下WWDC里的一个视频讲解:
WWDC讲解ro,rw链接

通过上面的视频我们知道,ro在编译的时候生成,在内存不够的时候就会进行移除,当要使用的时候就会重新从磁盘里去加载。在objc源码里我们发现有个叫class_rw_ext_t的结构体,简称为rwe。class_rw_ext_t这个也不是每个类里都生成的,由于生成class_rw_ext_t是有条件的:或者有分类,或者runtime API修改的时候会生成这个rwe结构体。runtime是无法修改成员变量的,rwe在对ro里进行拷贝出的也是其中一部分,一般ro里也就10%的内容需要修改。接着我们看rwe源码如下,也验证了我们这点:如果有rwe就直接返回里面的methods,没有就返回ro里的baseMethods。

   const method_array_t methods() const {
        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->methods;
        } else {
            return method_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseMethods};
        }
    }

属性存放在rw里源码:如果有rwe就直接返回里面的properties,没有就返回ro里的baseProperties。

        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->properties;
        } else {
            return property_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseProperties};
        }
    }

协议存放在rw里源码:如果有rwe就直接返回里面的protocols,没有就返回ro里的baseProtocols。

    const protocol_array_t protocols() const {
        auto v = get_ro_or_rwe();
        if (v.is<class_rw_ext_t *>()) {
            return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->protocols;
        } else {
            return protocol_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseProtocols};
        }
    }

苹果为什么要设计元类

设计元类只是单独为了存放我们的类方法吗?

实则其目的是为了复用消息机制。在OC中调⽤⽅法,实则是在给某个对象发送某条消息。
消息的发送在编译的时候编译器就会把⽅法转换为objc_msgSend这个函数。
id objc_msgSend(id self, SEL op, …) 这个函数有俩个隐式的参数:消息的接收者,消息的⽅法
名。通过这俩个参数就能去找到对应⽅法的实现。
objc_msgSend函数就会通过第⼀个参数消息的接收者的isa指针,找到对应的类,如果我们是通过
实例对象调⽤⽅法,那么这个isa指针就会找到实例对象的类对象,如果是类对象,就会找到类对
象的元类对象,然后再通过SEL⽅法名找到对应的imp,然后就能找到⽅法对应的实现。
那如果没有元类的话,那这个objc_msgSend⽅法还得多加俩个参数,⼀个参数⽤来判断这个⽅法
到底是类⽅法还是实例⽅法。⼀个参数⽤来判断消息的接受者到底是类对象还是实例对象。
消息的发送,越快越好。那如果没有元类,在objc_msgSend内部就会有有许多的判断,就会影响
消息的发送效率。
所以元类的出现就解决了这个问题,让各类各司其职,实例对象就⼲存储属性值的事,类对象存储
实例⽅法列表,元类对象存储类⽅法列表,符合设计原则中的单⼀职责,⽽且忽略了对对象类型的
判断和⽅法类型的判断可以⼤⼤的提升消息发送的效率,并且在不同种类的⽅法⾛的都是同⼀套流
程,在之后的维护上也⼤⼤节约了成本。
所以这个元类的出现,最⼤的好处就是能够复⽤消息传递这套机制。不管你是什么类型的⽅法,都
是同⼀套流程。

接着我们如何证明我们上面所说的呢?下面我们来看下在源码里设置断点调试:

第五篇:IOS类探究(成员变量值放在哪里?,成员变量信息放在哪里?)

第一我们打开objc的源码,

(lldb) x/4gx p.class
0x100008198: 0x0000000100008170 0x0000000100821140
0x1000081a8: 0x0001000100c04080 0x0002802900000000
(lldb) p/x 0x0000000100008170 & 0x007ffffffffffff8ULL
(unsigned long long) $1 = 0x0000000100008170
(lldb) po 0x0000000100008170
LGPerson

(lldb) p/x (class_data_bits_t *) 0x0000000100008190
(class_data_bits_t *) $3 = 0x0000000100008190
(lldb) p $3.data()
(class_rw_t *) $4 = 0x0000000100c040e0
  Fix-it applied, fixed expression was: 
(lldb) p $3.data()
(class_rw_t *) $4 = 0x0000000100c040e0
  Fix-it applied, fixed expression was: 
    $3->data()
(lldb) p $4->methods()
(const method_array_t) $5 = {
  list_array_tt<method_t, method_list_t, method_list_t_authed_ptr> = {
     = {
      list = {
        ptr = 0x0000000100003e30
      }
      arrayAndFlag = 4294983216
    }
  }
}
(lldb) p $5.list
(const method_list_t_authed_ptr<method_list_t>) $6 = {
  ptr = 0x0000000100003e30
}
(lldb) p $6.ptr
(method_list_t *const) $7 = 0x0000000100003e30
(lldb) p *$6
(objc_method_description)  $7 =(name = "classMethod",types = "v16@0:8")

我们一直按上面去打印,最后会打印一个classMethod这个类方法,在我们设置HPWPerson这个类里也有这个方法,如下,所以证明了类方法是存放在元类里面的。

@interface HPWPerson : NSObject {
    NSString *_hobby;
}

@property (nonatomic ,copy) NSString *name;
@property (nonatomic ,assign) int age;

- (void)instanceMethod;
+ (void)instanceMethod;
+ (void)classMethod;

接着我们看些runtime的api方法的实现:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取类的成员变量
    [self hpw_class_copyIvarList:HPWPerson.class];
    //获取类的属性
    [self hpw_class_copyPropertyList:HPWPerson.class];
    //获取类的方法
    [self hpw_class_copyMethodList:HPWPerson.class];
    
    [self methodTest:HPWPerson.class];
    
    [self impTest:HPWPerson.class];
}

//获取类的成员变量
-(void)hpw_class_copyIvarList:(Class)pClass {
    unsigned int  outCount = 0;
    Ivar *ivars = class_copyIvarList(pClass, &outCount);
    for (int i = 0; i < outCount; i ++) {
        Ivar ivar = ivars[i];
        const char *cName =  ivar_getName(ivar);
        const char *cType = ivar_getTypeEncoding(ivar);
        NSLog(@"name = %s type = %s",cName,cType);
    }
    free(ivars);
}

//获取类的属性
-(void)hpw_class_copyPropertyList:(Class)pClass {
    unsigned int outCount = 0;
    objc_property_t *perperties = class_copyPropertyList(pClass, &outCount);
    for (int i = 0; i < outCount; i++) {
        objc_property_t property = perperties[i];
        const char *cName = property_getName(property);
        const char *cType = property_getAttributes(property);
        NSLog(@"name = %s type = %s",cName,cType);
    }
    free(perperties);
}

//获取类的属性
-(void)hpw_class_copyMethodList:(Class)pClass {
    unsigned int outCount = 0;
    Method *methods = class_copyMethodList(pClass, &outCount);
    for (int i = 0; i < outCount; i++) {
        Method method = methods[i];
        NSString *name = NSStringFromSelector(method_getName(method));
        const char *cType = method_getTypeEncoding(method);
        NSLog(@"name = %@ type = %s",name,cType);
    }
    free(methods);
}

-(void)methodTest:(Class)pClass {
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    
    Method method1 = class_getInstanceMethod(pClass, @selector(instanceMethod));
    Method method2 = class_getInstanceMethod(metaClass, @selector(instanceMethod));
    Method method3 = class_getInstanceMethod(pClass, @selector(classMethod));
    Method method4 = class_getInstanceMethod(metaClass, @selector(classMethod));
    NSLog(@"%p - %p - %p - %p",method1,method2,method3,method4);
}

-(void)impTest:(Class)pClass {
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    
    IMP imp1 = class_getMethodImplementation(pClass, @selector(instanceMethod));
    IMP imp2 = class_getMethodImplementation(metaClass, @selector(instanceMethod));
    IMP imp3 = class_getMethodImplementation(pClass, @selector(classMethod));
    IMP imp4 = class_getMethodImplementation(metaClass, @selector(classMethod));
    NSLog(@"%p - %p - %p - %p",imp1,imp2,imp3,imp4);
  
}

2022-04-26 20:21:51.952225+0800 RuntimeApiTestDemo[50626:543107] name = _hobby type = @"NSString"
2022-04-26 20:21:51.952293+0800 RuntimeApiTestDemo[50626:543107] name = _age type = i
2022-04-26 20:21:51.952333+0800 RuntimeApiTestDemo[50626:543107] name = _name type = @"NSString"
2022-04-26 20:21:51.952374+0800 RuntimeApiTestDemo[50626:543107] name = name type = T@"NSString",C,N,V_name
2022-04-26 20:21:51.952415+0800 RuntimeApiTestDemo[50626:543107] name = age type = Ti,N,V_age
2022-04-26 20:21:51.952453+0800 RuntimeApiTestDemo[50626:543107] name = instanceMethod type = v16@0:8
2022-04-26 20:21:51.952485+0800 RuntimeApiTestDemo[50626:543107] name = name type = @16@0:8
2022-04-26 20:21:51.952518+0800 RuntimeApiTestDemo[50626:543107] name = setName: type = v24@0:8@16
2022-04-26 20:21:51.952554+0800 RuntimeApiTestDemo[50626:543107] name = age type = i16@0:8
2022-04-26 20:21:51.952586+0800 RuntimeApiTestDemo[50626:543107] name = setAge: type = v20@0:8i16
2022-04-26 20:21:51.952638+0800 RuntimeApiTestDemo[50626:543107] name = .cxx_destruct type = v16@0:8

上面这些我们是用runtime的api把成员变量,实例方法,类方法等打印出来。
通过上面总结:
1)ro里存放成员变量,实例方法,属性,协议,类对象
2)类方法存放在元类里面

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容