欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中的類、元類以及isa示例詳解

 更新時間:2018年01月11日 10:17:11   作者:FlyOceanFish  
從初學(xué)OC的時候就聽人提起過OC對象中的isa指針,用來指向?qū)ο笏鶎俚念?,從而可以在調(diào)用方法時通過isa指針找到相應(yīng)的方法和屬性,下面這篇文章主要給大家介紹了關(guān)于iOS中類、元類以及isa的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

對于類相信大家都知道是什么,如果看過runtime的源碼或者看過相關(guān)的文章對isa肯定也不陌生,不過元類(meta class)大家可能就比較陌生了。不過大家也不要擔心,我會細細道來,讓大家明白它到底是個什么東西。

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

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

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

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

2.如果沒找到,在相應(yīng)操作的對象中的方法列表中找調(diào)用的方法,如果找到,轉(zhuǎn)向相應(yīng)實現(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];

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

這張圖是非常精髓的,直接詮釋了元類和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.

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

  • 對于3、4次打印相同,就是因為NSObject元類的類是它本身.
  •  我們在實例化對象的時候,其實是創(chuàng)建了許多對象,這就是我們說的類簇。也對應(yī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**-----------------------

我只把重要的復(fù)制出來了,`NSObject`的所有的方法名沒有復(fù)制出來,在此處不是重要的。

此次打印結(jié)果的結(jié)論:

類方法(靜態(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

此次打印的結(jié)論:

  •  `Animal`相關(guān)打印的地址都比`Person`的大。再次詮釋了棧是由大往小排列的。??谠谧钚〉牡胤?/li>
  •  `Animal`和`Person`的`NSObject`的兩個地址一樣。(知道為什么大了嗎?其實就是保證這兩個地址足夠大,以致于永遠在棧中。這樣整個程序中其實就是存在一個,有點像單例的意思)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論