iOS中類、元類以及isa的示例分析

這篇文章將為大家詳細講解有關iOS中類、元類以及isa的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

營山網(wǎng)站建設公司創(chuàng)新互聯(lián)公司,營山網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為營山上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設要多少錢,請找那個售后服務好的營山做網(wǎng)站的公司定做!

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

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

為什么Person類名就能調(diào)用到alloc方法嗎?到底怎么找到了alloc的方法了呢?

1.首先,在相應操作的對象中的緩存方法列表中找調(diào)用的方法,如果找到,轉(zhuǎn)向相應實現(xiàn)并執(zhí)行。

2.如果沒找到,在相應操作的對象中的方法列表中找調(diào)用的方法,如果找到,轉(zhuǎn)向相應實現(xiàn)執(zhí)行

3.如果沒找到,去父類指針所指向的對象中執(zhí)行1,2.

4.以此類推,如果一直到根類還沒找到,轉(zhuǎn)向攔截調(diào)用,走消息轉(zhuǎn)發(fā)機制。

5.如果沒有重寫攔截調(diào)用的方法,程序報錯。

上邊是我從網(wǎng)上一篇文章摘錄的查找alloc的方法的大體過程。如果是實例方法(聲明以`-`開頭)這個描述的換個過程還是可以的,不過如果是類方法(聲明以`+`開頭比如`alloc`方法)還是有所欠缺的!

元類

`元類`也是類,是描述`Class `類對象的類。

Class aclass = [Person class];

>一切皆對象。每一個對象都對應一個類。 `Person` 類就是`person`變量對象的類,換句話說就是`person`對象的isa指向`Person`對應的結構體的類;`aclass`也是對象,描述它的類就是元類,換句話說`aclass`對象的isa指向的就是`元類`。
**元類保存了類方法的列表**。當一個類方法被調(diào)用時,元類會首先查找它本身是否有該類方法的實現(xiàn),如果沒有則該元類會向它的父類查找該方法,直到一直找到繼承鏈的頭。(回答文章上邊查找方法所欠缺的地方)

iOS中類、元類以及isa的示例分析

這張圖是非常精髓的,直接詮釋了元類和isa。大家可以一邊閱讀本文,一邊回憶此圖,多看幾遍。

上邊都是概念性質(zhì)偏多,不知道大家理解的如何?,F(xiàn)在看一個實例來具體介紹上邊的內(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;
}

這個示例有兩部分功能:

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到達過的地址的值:

  •  對象的地址是 0x100408400.

  •  類的地址是 0x100001350.

  •  元類的地址是 0x100001328.

  •  根元類(NSObject的元類)的地址是 0x7fffb9a4f0f0.

對于本次打印我們可以做出以下結論(可以再去看一遍上邊那張精髓的圖):

  • 對于3、4次打印相同,就是因為NSObject元類的類是它本身.

  •  我們在實例化對象的時候,其實是創(chuàng)建了許多對象,這就是我們說的類簇。也對應了我們在用runtime創(chuàng)建類的時候`objc_allocateClassPair(xx,xx)`中是`ClassPair`而不是`bjc_allocateClass`

  • 通過地址的大小也可以看出對象實例化先后,地址越小的越先實例化

  •  很好的詮釋了上邊那張精髓圖isa的指向

  • NSObject的兩個地址都非常大(哈哈哈哈哈!為什么非常大?。??接下往下看)

`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`的所有的方法名沒有復制出來,在此處不是重要的。

此次打印結果的結論:

類方法(靜態(tài)方法)是存儲在元類中的

觀察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

此次打印的結論:

  •  `Animal`相關打印的地址都比`Person`的大。再次詮釋了棧是由大往小排列的。棧口在最小的地方

  •  `Animal`和`Person`的`NSObject`的兩個地址一樣。(知道為什么大了嗎?其實就是保證這兩個地址足夠大,以致于永遠在棧中。這樣整個程序中其實就是存在一個,有點像單例的意思)

關于“iOS中類、元類以及isa的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

文章題目:iOS中類、元類以及isa的示例分析
分享地址:http://bm7419.com/article2/igeeoc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站內(nèi)鏈、軟件開發(fā)、服務器托管、小程序開發(fā)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設