iOS中的類、元類以及isa示例詳解
前言
對(duì)于類相信大家都知道是什么,如果看過(guò)runtime的源碼或者看過(guò)相關(guān)的文章對(duì)isa肯定也不陌生,不過(guò)元類(meta class)大家可能就比較陌生了。不過(guò)大家也不要擔(dān)心,我會(huì)細(xì)細(xì)道來(lái),讓大家明白它到底是個(gè)什么東西。
先看一段大家非常熟悉的代碼:
Person *person = [[Person alloc] init];
為什么Person類名就能調(diào)用到alloc方法嗎?到底怎么找到了alloc的方法了呢?
1.首先,在相應(yīng)操作的對(duì)象中的緩存方法列表中找調(diào)用的方法,如果找到,轉(zhuǎn)向相應(yīng)實(shí)現(xiàn)并執(zhí)行。
2.如果沒(méi)找到,在相應(yīng)操作的對(duì)象中的方法列表中找調(diào)用的方法,如果找到,轉(zhuǎn)向相應(yīng)實(shí)現(xiàn)執(zhí)行
3.如果沒(méi)找到,去父類指針?biāo)赶虻膶?duì)象中執(zhí)行1,2.
4.以此類推,如果一直到根類還沒(méi)找到,轉(zhuǎn)向攔截調(diào)用,走消息轉(zhuǎn)發(fā)機(jī)制。
5.如果沒(méi)有重寫攔截調(diào)用的方法,程序報(bào)錯(cuò)。
上邊是我從網(wǎng)上一篇文章摘錄的查找alloc的方法的大體過(guò)程。如果是實(shí)例方法(聲明以`-`開頭)這個(gè)描述的換個(gè)過(guò)程還是可以的,不過(guò)如果是類方法(聲明以`+`開頭比如`alloc`方法)還是有所欠缺的!
元類
`元類`也是類,是描述`Class `類對(duì)象的類。
Class aclass = [Person class];
>一切皆對(duì)象。每一個(gè)對(duì)象都對(duì)應(yīng)一個(gè)類。 `Person` 類就是`person`變量對(duì)象的類,換句話說(shuō)就是`person`對(duì)象的isa指向`Person`對(duì)應(yīng)的結(jié)構(gòu)體的類;`aclass`也是對(duì)象,描述它的類就是元類,換句話說(shuō)`aclass`對(duì)象的isa指向的就是`元類`。
**元類保存了類方法的列表**。當(dāng)一個(gè)類方法被調(diào)用時(shí),元類會(huì)首先查找它本身是否有該類方法的實(shí)現(xiàn),如果沒(méi)有則該元類會(huì)向它的父類查找該方法,直到一直找到繼承鏈的頭。(回答文章上邊查找方法所欠缺的地方)

這張圖是非常精髓的,直接詮釋了元類和isa。大家可以一邊閱讀本文,一邊回憶此圖,多看幾遍。
上邊都是概念性質(zhì)偏多,不知道大家理解的如何。現(xiàn)在看一個(gè)實(shí)例來(lái)具體介紹上邊的內(nèi)容。
代碼示例
// 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;
}
這個(gè)示例有兩部分功能:
1. 大家只看`Person`的演示功能即可。
2. 觀察Person和Animal兩個(gè)對(duì)象的打印(打印方法名的可以注釋掉,將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
我們來(lái)觀察isa到達(dá)過(guò)的地址的值:
- 對(duì)象的地址是 0x100408400.
- 類的地址是 0x100001350.
- 元類的地址是 0x100001328.
- 根元類(NSObject的元類)的地址是 0x7fffb9a4f0f0.
對(duì)于本次打印我們可以做出以下結(jié)論(可以再去看一遍上邊那張精髓的圖):
- 對(duì)于3、4次打印相同,就是因?yàn)镹SObject元類的類是它本身.
- 我們?cè)趯?shí)例化對(duì)象的時(shí)候,其實(shí)是創(chuàng)建了許多對(duì)象,這就是我們說(shuō)的類簇。也對(duì)應(yīng)了我們?cè)谟胷untime創(chuàng)建類的時(shí)候`objc_allocateClassPair(xx,xx)`中是`ClassPair`而不是`bjc_allocateClass`
- 通過(guò)地址的大小也可以看出對(duì)象實(shí)例化先后,地址越小的越先實(shí)例化
- 很好的詮釋了上邊那張精髓圖isa的指向
- NSObject的兩個(gè)地址都非常大(哈哈哈哈哈!為什么非常大????接下往下看)
`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**-----------------------
我只把重要的復(fù)制出來(lái)了,`NSObject`的所有的方法名沒(méi)有復(fù)制出來(lái),在此處不是重要的。
此次打印結(jié)果的結(jié)論:
類方法(靜態(tài)方法)是存儲(chǔ)在元類中的
觀察Person和Animal兩個(gè)對(duì)象的打印
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
此次打印的結(jié)論:
- `Animal`相關(guān)打印的地址都比`Person`的大。再次詮釋了棧是由大往小排列的。棧口在最小的地方
- `Animal`和`Person`的`NSObject`的兩個(gè)地址一樣。(知道為什么大了嗎?其實(shí)就是保證這兩個(gè)地址足夠大,以致于永遠(yuǎn)在棧中。這樣整個(gè)程序中其實(shí)就是存在一個(gè),有點(diǎn)像單例的意思)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS應(yīng)用UI開發(fā)中的字體和按鈕控件使用指南
這篇文章主要介紹了iOS應(yīng)用UI開發(fā)中的字體和按鈕控件使用指南,分別簡(jiǎn)單講解了UILabel和UIButton的用法,需要的朋友可以參考下2016-01-01
詳解iOS App開發(fā)中session和coockie的用戶數(shù)據(jù)存儲(chǔ)處理
iOS在HTTP網(wǎng)絡(luò)編程環(huán)境方面提供了NSURLSession、NSHTTPCookieStorage和NSHTTPCookie類來(lái)處理session和coockie的相關(guān)內(nèi)容,接下來(lái)我們將來(lái)詳解iOS App開發(fā)中session和coockie的用戶數(shù)據(jù)存儲(chǔ)處理:2016-06-06
解析iOS開發(fā)中的FirstResponder第一響應(yīng)對(duì)象
這篇文章主要介紹了解析iOS開發(fā)中的FirstResponder第一響應(yīng)對(duì)象,包括View的FirstResponder的釋放問(wèn)題,需要的朋友可以參考下2015-10-10
iOS實(shí)現(xiàn)電商購(gòu)物車界面示例
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)一個(gè)類似電商購(gòu)物車界面示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
僅幾行iOS代碼限制TextField輸入長(zhǎng)度
這篇文章主要為大家詳細(xì)介紹了通過(guò)幾行iOS代碼限制TextField輸入長(zhǎng)度的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS開發(fā)中CAlayer層的屬性以及自定義層的方法
這篇文章主要介紹了iOS開發(fā)中CAlayer層的屬性以及自定義層的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11

