Runtime

iOS中类、元类、isa详解

2021-03-10  本文已影响0人  YY110

From: http://www.cocoachina.com/articles/21805

本文是来自@FlyOceanFish的投稿

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

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

Person *person = [[Person alloc] init];

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

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

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

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

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

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

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

元类

元类也是类,是描述Class类对象的类。

Class aclass = [Person class];

一切皆对象。每一个对象都对应一个类。 Person 类就是person变量对象的类,换句话说就是person对象的isa指向Person对应的结构体的类;aclass也是对象,描述它的类就是元类,换句话说aclass对象的isa指向的就是元类

元类保存了类方法的列表。当一个类方法被调用时,元类会首先查找它本身是否有该类方法的实现,如果没有则该元类会向它的父类查找该方法,直到一直找到继承链的头。(回答文章上边查找方法所欠缺的地方)

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

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

代码示例

//  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的演示功能(不打印方法名称)

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到达过的地址的值:

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

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

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两个对象的打印

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

此次打印的结论:

[我的博客](http://flyoceanfish.top/

上一篇下一篇

猜你喜欢

热点阅读