美文网首页
Runtime之理解Instance、Class、MetaCla

Runtime之理解Instance、Class、MetaCla

作者: WavesFisherman | 来源:发表于2019-05-22 10:16 被阅读0次

    原文链接:https://www.jianshu.com/p/6b1192f6d671 转载请注明出处, 谢谢
    相信很多人都已经知道了他们三者的关系了, 下面借用一张图

    来源: http://www.sealiesoftware.com/blog/class%20diagram.pdf
    我们先来动态创建一个类
    //动态创建一个新类
    - (void)createNewClass{
        Class newClass = [self createNewClass:"SubError"];
        //创建这个类的实例对象
        id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil];
        //调用方法
        SEL sel = NSSelectorFromString(@"TestMetaClass"); // 获取定义的方法
        //这里报错, Build Setting -> apple clang preprocessing -> Enable Strict Checking of objc_msgSend Calls 设置成NO
        id result = objc_msgSend(instance,sel); //调用该方法
    }
    
    //创建的这个类继承于NSError
    - (Class)createNewClass:(char *)className{
        //创建一个NSError的子类 SubError
        Class newClass = objc_allocateClassPair([NSError class], className, 0);
        //添加实例对象方法 IMP函数指针
        SEL sel = sel_registerName("TestMetaClass"); //注册一个方法选择器 不能重复注册同一个选择器
        class_addMethod(newClass, sel, (IMP)TestMetaClass, "v@:");
        //注册这个类
        objc_registerClassPair(newClass);
        return newClass;
    }
    
    //实例对象、类、元类等的关系
    void TestMetaClass(id self, SEL _cmd) {
        
        [self setValue:@"zhang" forKey:@"name"];
        NSLog(@"This objcet is %p", self); // TestClass类的实例对象
        
        //获取类的名称
        const char *className1 = class_getName([self class]);
        const char *className2 = object_getClassName([self class]);
        NSString *className3 = NSStringFromClass([self class]);
        NSLog(@"ClassName is %s, %s, %@", className1, className2, className3);
        //获取类
        Class class1 = [self class];//调用实例对象的方法
        Class class2 = object_getClass(self);//根据实例对象获取类
        Class class3 = objc_getClass(className1);//根据类名获取类
        NSLog(@"Class is %p, %p, %p", class1, class2, class3);
        //父类
        Class superClass1 = class_getSuperclass(class1);
        Class superClass2 = [self superclass];
        NSLog(@"SupperClass is %p, %p", superClass1, superClass2);
        
        //获取元类
        Class metaClass = objc_getMetaClass(className1);
        
        //判断是否为元类
        BOOL isMetaClass = class_isMetaClass(metaClass);
        
        if (isMetaClass) {
            NSLog(@"%@ is meta class", metaClass);
        }
        NSLog(@"NSError's metaClass is %@", objc_getMetaClass(object_getClassName([NSError class])));
        NSLog(@"NSError's metaClass is %p", objc_getMetaClass(object_getClassName([NSError class])));
        NSLog(@"Class is %@, super class is %@, meta class is %@, metaclass's superclass is %@", [self class], [self superclass], metaClass, class_getSuperclass(metaClass));
        NSLog(@"Class is %p, super class is %p, meta class is %p, metaclass's superclass is %p", [self class], [self superclass], metaClass, class_getSuperclass(metaClass));
        //获取实例的大小
        size_t size = class_getInstanceSize(class1);
        NSLog(@"%zu",size);    
    }
    

    忽略其他的日志输出

    2019-05-22 09:40:38.779045+0800 RuntimeDemo[996:17329] This objcet is 0x6000015de730
    2019-05-22 09:40:38.779215+0800 RuntimeDemo[996:17329] ClassName is SubError, SubError, SubError
    2019-05-22 09:40:38.779313+0800 RuntimeDemo[996:17329] Class is 0x6000015de580, 0x6000015de580, 0x6000015de580
    2019-05-22 09:40:38.779391+0800 RuntimeDemo[996:17329] SupperClass is 0x10072c208, 0x10072c208
    2019-05-22 09:40:38.779467+0800 RuntimeDemo[996:17329] SubError is meta class
    2019-05-22 09:40:38.779542+0800 RuntimeDemo[996:17329] NSError's metaClass is NSError
    2019-05-22 09:40:38.779608+0800 RuntimeDemo[996:17329] NSError's metaClass is 0x10072c258
    2019-05-22 09:40:38.779692+0800 RuntimeDemo[996:17329] Class is SubError, super class is NSError, meta class is SubError, metaclass's superclass is NSError
    2019-05-22 09:40:38.779800+0800 RuntimeDemo[996:17329] Class is 0x6000015de580, super class is 0x10072c208, meta class is 0x6000015de1c0, metaclass's superclass is 0x10072c258
    2019-05-22 09:40:38.779875+0800 RuntimeDemo[996:17329] 48
    

    这个instance是SubError类的实例对象, SubError类的父类是NSError, SubError类的元类是SubError, NSError类的元类是NSError, SubError元类的父类是NSError元类,具体的你要理解isa指针, 下面我们再来看NSObject
    我们先来一组错误的案例

    NSLog(@"NSObject's class is %p", [NSObject class]);
    NSLog(@"NSObject's meta class is %p", objc_getClass((__bridge void *)[NSObject class]));
    NSLog(@"NSObject's class is %p", objc_getClass("NSObject"));//object_getClassName([NSObject class])
    NSLog(@"NSObject's meta class is %p", objc_getMetaClass("NSObject"));
    
    2019-05-22 09:52:42.129790+0800 RuntimeDemo[1114:23535] NSObject's class is 0x10f389ec8
    2019-05-22 09:52:42.129895+0800 RuntimeDemo[1114:23535] NSObject's meta class is 0x0
    2019-05-22 09:52:42.129971+0800 RuntimeDemo[1114:23535] NSObject's class is 0x10f389ec8
    2019-05-22 09:52:42.130053+0800 RuntimeDemo[1114:23535] NSObject's meta class is 0x10f389e78
    

    网上有好多博客是这么说明NSObject自身和元类的关系的, 但是你仔细看输出结果的第二句和第四句, 打印的地址不一致, 而且他们认为objc_getClass这个方法返回的是元类, 是不是元类可以用系统的方法来判断, 下面来看一下我的做法

    NSObject *obj = [[NSObject alloc] init];
    NSLog(@"instance is %p", obj);
    Class objClass = [obj class];
    const char *className = class_getName([obj class]);
    Class objSuperClass = class_getSuperclass(objClass);
    Class metaClass = objc_getMetaClass(className);
    if (class_isMetaClass(metaClass)) {
        NSLog(@"%@ is meta class, %p", metaClass, metaClass);
    }
    NSLog(@"MetaClass name is %s", class_getName(metaClass));
    NSLog(@"Class is %p, super class is %p, meta class is %p, metaclass's superclass is %p", objClass, objSuperClass, metaClass, class_getSuperclass(metaClass));
    
    2019-05-22 10:10:23.803262+0800 RuntimeDemo[1313:32937] instance is 0x600003384370
    2019-05-22 10:10:23.803389+0800 RuntimeDemo[1313:32937] NSObject is meta class, 0x1095dde78
    2019-05-22 10:10:23.803447+0800 RuntimeDemo[1313:32937] MetaClass name is NSObject
    2019-05-22 10:10:23.803519+0800 RuntimeDemo[1313:32937] Class is 0x1095ddec8, super class is 0x0, meta class is 0x1095dde78, metaclass's superclass is 0x1095ddec8
    

    从打印输出的结果可以看出, 使用class_getMetaClass方法可以可以获取到元类, NSObject的父类是nil, 元类是NSObject元类, NSObject元类的父类是NSObject自身, 这样就符合上图的指向逻辑了, 最后形成了一个闭环
    这里我们可以猜测(__bridge void *)的作用并不是像别人说的(类型转换 转换成id), 而是转换成了(void *)空指针类型也就是nil

    参考文献:
    http://blog.devtang.com/2013/10/15/objective-c-object-model/
    http://southpeak.github.io/2014/10/25/objective-c-runtime-1/

    相关文章

      网友评论

          本文标题:Runtime之理解Instance、Class、MetaCla

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