前言
對于類相信大家都知道是什么,如果看過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。大家可以一邊閱讀本文,一邊回憶此圖,多看幾遍。
上邊都是概念性質偏多,不知道大家理解的如何?,F在看一個實例來具體介紹上邊的內容。
代碼示例
// 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]));}@endint 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 0x100001328Following the isa pointer 1 times gives 0x100001350Following the isa pointer 2 times gives 0x100001328Following the isa pointer 3 times gives 0x7fffb9a4f0f0Following the isa pointer 4 times gives 0x7fffb9a4f0f0NSObject's class is 0x7fffb9a4f140NSObject's meta class is 0x7fffb9a4f0f0
我們來觀察isa到達過的地址的值:
對于本次打印我們可以做出以下結論(可以再去看一遍上邊那張精髓的圖):
`Person`的演示功能(打印方法名稱)
Class is Person, and super is NSObject.MetaClass is 0x100002378Following 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 0x100001338Following the isa pointer 1 times gives 0x100001360Following the isa pointer 2 times gives 0x100001338Following the isa pointer 3 times gives 0x7fffb9a4f0f0Following the isa pointer 4 times gives 0x7fffb9a4f0f0NSObject's class is 0x7fffb9a4f140NSObject's meta class is 0x7fffb9a4f0f0--------------------------------This object is 0x100675ed0.Class is Animal, and super is NSObject.MetaClass is 0x100001388Following the isa pointer 1 times gives 0x1000013b0Following the isa pointer 2 times gives 0x100001388Following the isa pointer 3 times gives 0x7fffb9a4f0f0Following the isa pointer 4 times gives 0x7fffb9a4f0f0NSObject's class is 0x7fffb9a4f140NSObject's meta class is 0x7fffb9a4f0f0Program ended with exit code: 0
此次打印的結論:
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答