美文网首页
类的结构分析(二)

类的结构分析(二)

作者: _涼城 | 来源:发表于2020-09-19 11:16 被阅读0次

类的结构分析 中属性列表和方法列表分析可得出property_list中只有属性,没有成员变量,entsize_list中仅有实例方法,没有类方法,那么问题来了,成员变量存储在哪里?类方法存储在哪里?

探索成员变量
成员变量与属性的区别

是否具有系统生成的setter/getter方法。

成员变量与实例变量的区别

实例变量是一种特殊的成员变量(是类的实例化生成的变量)。

成员变量的存储

class_rw_t下有一个结构体为class_ro_t,在class_ro_t下有一个ivars

struct class_ro_t {
    ...
    const ivar_list_t * ivars;
    ...
}

ivars是一个数组指针,存放着类的成员变量。

ivars.png
SEl与IMP

 SEL(类似于方法名称)是Objective-C 基于C 语言 IMP(类似于索引)函数指针的上层封装,通过IMP最终找到方法的实现。

static struct /*_method_list_t*/ {
    unsigned int entsize;  // sizeof(struct _objc_method)
    unsigned int method_count;
    struct _objc_me
thod method_list[8];
} _OBJC_$_INSTANCE_METHODS_Person __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_objc_method),
    8,
    {{(struct objc_selector *)"nickName", "@16@0:8", (void *)_I_Person_nickName},
    {(struct objc_selector *)"setNickName:", "v24@0:8@16", (void *)_I_Person_setNickName_},
    {(struct objc_selector *)"name", "@16@0:8", (void *)_I_Person_name},
    {(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_Person_setName_},
    {(struct objc_selector *)"nickName", "@16@0:8", (void *)_I_Person_nickName},
    {(struct objc_selector *)"setNickName:", "v24@0:8@16", (void *)_I_Person_setNickName_},
    {(struct objc_selector *)"name", "@16@0:8", (void *)_I_Person_name},
    {(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_Person_setName_}}
};

clang编码后的main.cpp中,我们找到下面这样的代码,那么@16@0:8代表什么意思呢?

类型编码

ivar_getTypeEncoding

代码,输出结果可验证ivar_类型编码表_)内的信息

void getEncodeTypes(){
    NSLog(@"char --> %s",@encode(char));
    NSLog(@"int --> %s",@encode(int));
    NSLog(@"short --> %s",@encode(short));
    NSLog(@"long --> %s",@encode(long));
    NSLog(@"long long --> %s",@encode(long long));
    NSLog(@"unsigned char --> %s",@encode(unsigned char));
    NSLog(@"unsigned int --> %s",@encode(unsigned int));
    NSLog(@"unsigned short --> %s",@encode(unsigned short));
    NSLog(@"unsigned long --> %s",@encode(unsigned long long));
    NSLog(@"float --> %s",@encode(float));
    NSLog(@"bool --> %s",@encode(bool));
    NSLog(@"void --> %s",@encode(void));
    NSLog(@"char * --> %s",@encode(char *));
    NSLog(@"id --> %s",@encode(id));
    NSLog(@"Class --> %s",@encode(Class));
    NSLog(@"SEL --> %s",@encode(SEL));
    int array[] = {1,2,3};
    NSLog(@"int[] --> %s",@encode(typeof(array)));
    typedef struct person{
        char *name;
        int age;
    }Person;
    NSLog(@"struct --> %s",@encode(Person));

    typedef union union_type{
        char *name;
        int a;
    }Union;
    NSLog(@"union --> %s",@encode(Union));

    int a = 2;
    int *b = {&a};
    NSLog(@"int[] --> %s",@encode(typeof(b)));
}

因此,@16@0:8

  • 第一个@返回id类型,
  • 16,共用16字节,
  • 第二个@表示参数为id类型,
  • 0,表示第一个参数当前从0位置开始,
  • :,表示为sel
  • 8,表示第二个参数从8位置开始

类似地,prop_list_t下的"nickName","T@\NSString\",C,N,V_nickName",同样可以按照着类型编码进行翻译。

static struct /*_prop_list_t*/ {
    unsigned int entsize;  // sizeof(struct _prop_t)
    unsigned int count_of_properties;
    struct _prop_t prop_list[2];
} _OBJC_$_PROP_LIST_Person __attribute__ ((used, section ("__DATA,__objc_const"))) = {
    sizeof(_prop_t),
    2,
    {{"nickName","T@\"NSString\",C,N,V_nickName"},
    {"name","T@\"NSString\",&,N,V_name"}}
};
探索类方法

在类的信息中只读到了实例方法,并没有读到类方法的信息,类->实例,那是不是类方法存储在元类当中呢?

通过lldb调试

先获取到元类,在元类中找到methods

元类的methods.png
通过class_copyMethodList调试
Method *
class_copyMethodList(Class cls, unsigned int *outCount)

通过class_copyMethodList可以获取当前类的方法列表,我们传入元类

void Objc_copyMethodList(Class pClass){
    unsigned int count = 0;
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);

    Method *methods = class_copyMethodList(metaClass, &count);
    for (unsigned int i=0; i < count; i++) {
        Method const method = methods[I];
        //获取方法名
        NSString *key = NSStringFromSelector(method_getName(method));

       NSLog(@"Method, name: %@", key);
    }
    free(methods);
}

最终输出Method, name: testClassMethod

通过class_getInstanceMethod调试
void InstanceMethod_classToMetaclass(Class pClass){

    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);

    Method method1 = class_getInstanceMethod(pClass, @selector(say));
    Method method2 = class_getInstanceMethod(metaClass, @selector(say));

    Method method3 = class_getInstanceMethod(pClass, @selector(testClassMethod));
    Method method4 = class_getInstanceMethod(metaClass, @selector(testClassMethod));

    NSLog(@"%s - %p-%p-%p-%p",__func__,method1,method2,method3,method4);
}

最终输出InstanceMethod_classToMetaclass - 0x1000081d8-0x0-0x0-0x100008170。得出结果,类可以找到实例方法,元类以找到类方法。

通过class_getClassMethod调试

查看class_getClassMethod的源码,我们可以发现也是通过 元类cls->getMeta()进行查找,本质也是使用class_getInstanceMethod

Method class_getClassMethod(Class cls, SEL sel)
{
    if (!cls  ||  !sel) return nil;

    return class_getInstanceMethod(cls->getMeta(), sel);
}
问:下面代码的最终输出结果?
void ClassMethod_classToMetaclass(Class pClass){
    
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    Method method1 = class_getClassMethod(pClass, @selector(say));
    Method method2 = class_getClassMethod(metaClass, @selector(say));
    Method method3 = class_getClassMethod(pClass, @selector(testClassMethod));
    Method method4 = class_getClassMethod(metaClass, @selector(testClassMethod));
    
    NSLog(@"%s-%p-%p-%p-%p",__func__,method1,method2,method3,method4);
}

由于say为实例方法,因此,method1method2都为0x0;

testClassMethod为Person类的实例方法,因此method3有值;

由于Person的元类还是元类本身,因此method4也有值。

相关文章

网友评论

      本文标题:类的结构分析(二)

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