当前位置:首页 > 通信资讯 > 正文

ios 类 元类(ios isa指针)

前言

对于类相信大家都知道是什么,如果看过runtime的源码或者看过相关的文章对isa肯定也不陌生,不过元类(meta class)大家可能就比较陌生了。不过大家也不要担心,我会细细道来,让大家明白它到底是个什么东西。

先看一段大家非常熟悉的代码:

?
1 person *person = [[person alloc] init];

为什么person类名就能调用到alloc方法吗?到底怎么找到了alloc的方法了呢?

1.首先,在相应操作的对象中的缓存方法列表中找调用的方法,如果找到,转向相应实现并执行。

2.如果没找到,在相应操作的对象中的方法列表中找调用的方法,如果找到,转向相应实现执行

3.如果没找到,去父类指针所指向的对象中执行1,2.

4.以此类推,如果一直到根类还没找到,转向拦截调用,走消息转发机制。

5.如果没有重写拦截调用的方法,程序报错。

上边是我从网上一篇文章摘录的查找alloc的方法的大体过程。如果是实例方法(声明以`-`开头)这个描述的换个过程还是可以的,不过如果是类方法(声明以`+`开头比如`alloc`方法)还是有所欠缺的!

元类

`元类`也是类,是描述`class `类对象的类。

?
1 class aclass = [person class];

>一切皆对象。每一个对象都对应一个类。 `person` 类就是`person`变量对象的类,换句话说就是`person`对象的isa指向`person`对应的结构体的类;`aclass`也是对象,描述它的类就是元类,换句话说`aclass`对象的isa指向的就是`元类`。
**元类保存了类方法的列表**。当一个类方法被调用时,元类会首先查找它本身是否有该类方法的实现,如果没有则该元类会向它的父类查找该方法,直到一直找到继承链的头。(回答文章上边查找方法所欠缺的地方)

ios 类 元类(ios isa指针)

这张图是非常精髓的,直接诠释了元类和isa。大家可以一边阅读本文,一边回忆此图,多看几遍。

上边都是概念性质偏多,不知道大家理解的如何。现在看一个实例来具体介绍上边的内容。

代码示例

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 // created by flyoceanfish on 2018/1/9. // copyright © 2018年 flyoceanfish. all rights reserved. // #import #import @interface person: nsobject @end @implementation person + (void)printstatic{ } - (void)print{ nslog(@"this object is %p.", self); nslog(@"class is %@, and super is %@.", [self class], [self superclass]); const char *name = object_getclassname(self); class metaclass = objc_getmetaclass(name); nslog(@"metaclass is %p",metaclass); class currentclass = [self class]; for (int i = 1; i < 5; i++) { nslog(@"following the isa pointer %d times gives %p", i, currentclass); unsigned int countmethod = 0; nslog(@"---------------**%d start**-----------------------",i); method * methods = class_copymethodlist(currentclass, &countmethod); [self printmethod:countmethod methods:methods ]; nslog(@"---------------**%d end**-----------------------",i); currentclass = object_getclass(currentclass); } nslog(@"nsobject's class is %p", [nsobject class]); nslog(@"nsobject's meta class is %p", object_getclass([nsobject class])); } - (void)printmethod:(int)count methods:(method *) methods{ for (int j = 0; j < count; j++) { method method = methods[j]; sel methodsel = method_getname(method); const char * selname = sel_getname(methodsel); if (methodsel) { nslog(@"sel------%s", selname); } } } @end @interface animal: nsobject @end @implementation animal - (void)print{ nslog(@"this object is %p.", self); nslog(@"class is %@, and super is %@.", [self class], [self superclass]); const char *name = object_getclassname(self); class metaclass = objc_getmetaclass(name); nslog(@"metaclass is %p",metaclass); class currentclass = [self class]; for (int i = 1; i < 5; i++) { nslog(@"following the isa pointer %d times gives %p", i, currentclass); currentclass = object_getclass(currentclass); } nslog(@"nsobject's class is %p", [nsobject class]); nslog(@"nsobject's meta class is %p", object_getclass([nsobject class])); } @end int main(int argc, const char * argv[]) { @autoreleasepool { person *person = [[person alloc] init]; class class = [person class]; [person print]; // printf("-------------------------------- "); // animal *animal = [[animal alloc] init]; // [animal print]; } return 0; }

这个示例有两部分功能:

1. 大家只看`person`的演示功能即可。

2. 观察person和animal两个对象的打印(打印方法名的可以注释掉,将main方法中的代码注释打开)

`person`的演示功能(不打印方法名称)

?
1 2 3 4 5 6 7 8 9 this object is 0x100408400. class is person, and super is nsobject. metaclass is 0x100001328 following the isa pointer 1 times gives 0x100001350 following the isa pointer 2 times gives 0x100001328 following the isa pointer 3 times gives 0x7fffb9a4f0f0 following the isa pointer 4 times gives 0x7fffb9a4f0f0 nsobject's class is 0x7fffb9a4f140 nsobject's meta class is 0x7fffb9a4f0f0

我们来观察isa到达过的地址的值:

  • 对象的地址是 0x100408400.
  • 类的地址是 0x100001350.
  • 元类的地址是 0x100001328.
  • 根元类(nsobject的元类)的地址是 0x7fffb9a4f0f0.

对于本次打印我们可以做出以下结论(可以再去看一遍上边那张精髓的图):

  • 对于3、4次打印相同,就是因为nsobject元类的类是它本身.
  • 我们在实例化对象的时候,其实是创建了许多对象,这就是我们说的类簇。也对应了我们在用runtime创建类的时候`objc_allocateclasspair(xx,xx)`中是`classpair`而不是`bjc_allocateclass`
  • 通过地址的大小也可以看出对象实例化先后,地址越小的越先实例化
  • 很好的诠释了上边那张精髓图isa的指向
  • nsobject的两个地址都非常大(哈哈哈哈哈!为什么非常大啊??接下往下看)

`person`的演示功能(打印方法名称)

?
1 2 3 4 5 6 7 8 9 10 11 12 13 class is person, and super is nsobject. metaclass is 0x100002378 following the isa pointer 1 times gives 0x1000023a0 ---------------**1 start**----------------------- sel------printmethod:methods: sel------print ---------------**1 end**----------------------- following the isa pointer 2 times gives 0x100002378 ---------------**2 start**----------------------- sel------printstatic ---------------**2 end**----------------------- following the isa pointer 3 times gives 0x7fffb9a4f0f0 ---------------**3 start**-----------------------

我只把重要的复制出来了,`nsobject`的所有的方法名没有复制出来,在此处不是重要的。

此次打印结果的结论:

类方法(静态方法)是存储在元类中的

观察person和animal两个对象的打印

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 this object is 0x100508e70. class is person, and super is nsobject. metaclass is 0x100001338 following the isa pointer 1 times gives 0x100001360 following the isa pointer 2 times gives 0x100001338 following the isa pointer 3 times gives 0x7fffb9a4f0f0 following the isa pointer 4 times gives 0x7fffb9a4f0f0 nsobject's class is 0x7fffb9a4f140 nsobject's meta class is 0x7fffb9a4f0f0 -------------------------------- this object is 0x100675ed0. class is animal, and super is nsobject. metaclass is 0x100001388 following the isa pointer 1 times gives 0x1000013b0 following the isa pointer 2 times gives 0x100001388 following the isa pointer 3 times gives 0x7fffb9a4f0f0 following the isa pointer 4 times gives 0x7fffb9a4f0f0 nsobject's class is 0x7fffb9a4f140 nsobject's meta class is 0x7fffb9a4f0f0 program ended with exit code: 0

此次打印的结论:

  • `animal`相关打印的地址都比`person`的大。再次诠释了栈是由大往小排列的。栈口在最小的地方
  • `animal`和`person`的`nsobject`的两个地址一样。(知道为什么大了吗?其实就是保证这两个地址足够大,以致于永远在栈中。这样整个程序中其实就是存在一个,有点像单例的意思)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://www.cocoachina.com/ios/20180110/21805.html

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。