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指向的就是`元類`。
**元類保存了類方法的列表**。當一個類方法被調用時,元類會首先查找它本身是否有該類方法的實現,如果沒有則該元類會向它的父類查找該方法,直到一直找到繼承鏈的頭。(回答文章上邊查找方法所欠缺的地方)
這張圖是非常精髓的,直接詮釋了元類和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`的兩個地址一樣。(知道為什麽大了嗎?其實就是保證這兩個地址足夠大,以致於永遠在棧中。這樣整個程序中其實就是存在一個,有點像單例的意思)
iOS中類、元類、isa詳解