Runtime 中关于MetaClass的问题

2018-02-27 11:09:43来源:https://www.jianshu.com/p/d0c6a3efb4d4作者:阳光下的小泡沫丶人点击

分享


前阵子我学习了Objective-C中的Runtime库,最近我一个同学也在学习Runtime,和我看的是同一篇博客(博客地址),他学习过程时遇到了一个问题,就过来问我。


问题是博客中关于metaclass 的一段代码


代码如下:


void TestMetaClass(id self, SEL _cmd) {
NSLog(@"This objcet is %p", self);
NSLog(@"Class is %@, super class is %@", [self class], [self superclass]);
Class currentClass = [self class];
for (int i = 0; i < 4; i++) {
NSLog(@"Following the isa pointer %d times gives %p", i, currentClass);
currentClass = objc_getClass((__bridge void *)currentClass);
}
NSLog(@"NSObject's class is %p", [NSObject class]);
NSLog(@"NSObject's meta class is %p", objc_getClass((__bridge void *)[NSObject class]));
}
#pragma mark -
@implementation Test
- (void)ex_registerClassPair {
Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0);
class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:");
objc_registerClassPair(newClass);
id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil];
[instance performSelector:@selector(testMetaClass)];
}
@end

这段代码的功能就是通过运行时动态创建了一个类叫TestClass继承自系统的NSError类,然后给这个类添加一个方法实现是TestMetaClass在这个方法中来测试metaclass的相关内容。


运行结果是


2014-10-20 22:57:07.352 mountain[1303:41490] This objcet is 0x7a6e22b0
2014-10-20 22:57:07.353 mountain[1303:41490] Class is TestStringClass, super class is NSError
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 0 times gives 0x7a6e21b0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 1 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 2 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 3 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] NSObject's class is 0xe10000
2014-10-20 22:57:07.354 mountain[1303:41490] NSObject's meta class is 0x0

在博客中博主写到



我们在for循环中,我们通过objc_getClass来获取对象的isa,并将其打印出来,依此一直回溯到NSObject的meta-class。分析打印结果,可以看到最后指针指向的地址是0x0,即NSObject的meta-class的类地址。



我同学就觉得这段代码理解起来有问题,我当时学习的时候也敲了这段代码,但是运行结果和博主一样就没怎么在意,我现在回过头来看这段代码是觉得有点问题。


首先,我觉得objc_getClass((__bridge void *)currentClass)这段代码的写法有问题,在objc/runtime.h中这个方法的定义是:


OBJC_EXPORT Class _Nullable
objc_getClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

从中我们可以看见这个方法传入的参数是一个const char *类型,而博主的用法是传入了(__bridge void *)currentClass把currentClass转化成void *的类型传入了方法,虽然这样子代码是能运行的,但是我认为用法是错误的。


然后,我认为objc_getClass()这个方法时不能得到一个类的isa指针的,这个方法的作用只是根据一个char *类型的字符数组来获取到一个类,而在objc/runtime.h中给出了另一个方法


OBJC_EXPORT Class _Nullable
objc_getMetaClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

这个方法才能得到metaclass,于是我尝试了下面的代码


    NSLog(@"NSObject's class is %p", [NSObject class]);
NSLog(@"NSObject's meta class is %p", objc_getMetaClass("NSObject"));

发现输出为:


2017-11-28 12:49:19.611849+0800 meta-class[4153:64693388] NSObject's class is 0x7fffc271d140
2017-11-28 12:49:19.611855+0800 meta-class[4153:64693388] NSObject's meta class is 0x7fffc271d0f0

NSObjectmetaclass是有地址的,而并不是博主所说的NSObject的metaclass类地址是0x0,博主代码运行时得到的地址为0x0只是方法传入的参数不对,返回值是nil,所以地址是0x0。


所以我认为,所有类和它的meta-class都是有地址的(包括NSObject),不然运行时如果metaclass没地址,我们调用类方法是去哪里找到的方法。


于是我将代码改成如下


void TestMetaClass(id self, SEL _cmd) {
NSLog(@"this object is %p", self);
NSLog(@"class is %p, super class is %p, meta-class is %p",[self class],[self superclass], objc_getMetaClass("TestClass"));
Class currentClass = [self class];
for (int i = 0; i < 4; i++) {
NSLog(@"Following the isa pointer %d times gives %p name is %s is meta-class %d",i,[currentClass class],class_getName(currentClass),class_isMetaClass(currentClass));
currentClass = objc_getMetaClass(class_getName(currentClass));
}

NSLog(@"NSObject's class is %p", [NSObject class]);
NSLog(@"NSObject's meta class is %p", objc_getMetaClass("NSObject"));
}
@implementation Test
- (void)ex_registerClassPair {

Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0);
class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:");
objc_registerClassPair(newClass);

id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil];
[instance performSelector:@selector(testMetaClass) ];
}

输出为


2017-11-28 13:36:07.804037+0800 meta-class[5651:64868327] this object is 0x100408f20
2017-11-28 13:36:07.804172+0800 meta-class[5651:64868327] class is 0x100408d50, super class is 0x7fffbf5bf798, meta-class is 0x100408d80
2017-11-28 13:36:07.804185+0800 meta-class[5651:64868327] Following the isa pointer 0 times gives 0x100408d50 name is TestClass is meta-class 0
2017-11-28 13:36:07.804194+0800 meta-class[5651:64868327] Following the isa pointer 1 times gives 0x100408d80 name is TestClass is meta-class 1
2017-11-28 13:36:07.804200+0800 meta-class[5651:64868327] Following the isa pointer 2 times gives 0x100408d80 name is TestClass is meta-class 1
2017-11-28 13:36:07.804206+0800 meta-class[5651:64868327] Following the isa pointer 3 times gives 0x100408d80 name is TestClass is meta-class 1
2017-11-28 13:36:07.804212+0800 meta-class[5651:64868327] NSObject's class is 0x7fffc271d140
2017-11-28 13:36:07.804217+0800 meta-class[5651:64868327] NSObject's meta class is 0x7fffc271d0f0

可以看到TestClass 和NSObject 的metaclass都是有地址的。
但是currentClass = objc_getMetaClass(class_getName(currentClass));这样写似乎只能获取到TestClass的metaclass而并不能再去获取到metaclass的metaclass毕竟这个方法的返回值的metaclass而不是isa指向的class。


综上所述,一个类和它的metaclass都是有地址的(包括NSObject)。


更新

经评论区的小伙伴SamZL指出


可以通过object_getClass()方法得到类的isa指针。


这样我们的代码就修改为


void TestMetaClass(id self, SEL _cmd) {
NSLog(@"this object is %p", self);
NSLog(@"class is %p, super class is %p, meta-class is %p",[self class],[self superclass], objc_getMetaClass("TestClass"));
Class currentClass = [self class];
for (int i = 0; i < 4; i++) {
NSLog(@"Following the isa pointer %d times gives %p name is %s is %@ meta-class ",i,[currentClass class],class_getName(currentClass),(class_isMetaClass(currentClass) ? @"" : @"not"));
currentClass = object_getClass(currentClass);
}

NSLog(@"NSObject's class is %p", [NSObject class]);
NSLog(@"NSObject's meta class is %p", objc_getMetaClass("NSObject"));
}

得到的输出为


2017-12-12 11:34:21.306288+0800 meta-class[36868:90457953] this object is 0x100502c00
2017-12-12 11:34:21.306428+0800 meta-class[36868:90457953] class is 0x100502a10, super class is 0x7fffbf5bf798, meta-class is 0x100502a40
2017-12-12 11:34:21.306450+0800 meta-class[36868:90457953] Following the isa pointer 0 times gives 0x100502a10 name is TestClass is not meta-class
2017-12-12 11:34:21.306460+0800 meta-class[36868:90457953] Following the isa pointer 1 times gives 0x100502a40 name is TestClass is meta-class
2017-12-12 11:34:21.306467+0800 meta-class[36868:90457953] Following the isa pointer 2 times gives 0x7fffc271d0f0 name is NSObject is meta-class
2017-12-12 11:34:21.306474+0800 meta-class[36868:90457953] Following the isa pointer 3 times gives 0x7fffc271d0f0 name is NSObject is meta-class
2017-12-12 11:34:21.306480+0800 meta-class[36868:90457953] NSObject's class is 0x7fffc271d140
2017-12-12 11:34:21.306486+0800 meta-class[36868:90457953] NSObject's meta class is 0x7fffc271d0f0
Program ended with exit code: 0

这样就和我们所理解的isa指针的指向关系相同了。


关系如下图


449095-3e972ec16703c54d.png

有问题欢迎在评论区指出哦。








最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台