Runtime-內(nèi)存模型

Runtime 是 iOS編程人員的核心基礎(chǔ)知識(shí),Objc Runtime使得C具有了面向?qū)ο竽芰?,在程序運(yùn)行時(shí)創(chuàng)建,檢查,修改類(lèi)、對(duì)象和它們的方法。
runtime是開(kāi)源的,GitHub 有可調(diào)式的源碼 objc4_debug(待工程配置過(guò)程??),也可以去官網(wǎng)找 objc4.
學(xué)習(xí)參考 蘋(píng)果官方的Runtime編程指南

isa & id

第一印象指針,在<objc.h>中可以看到:

/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

/// Represents an instance of a class.
struct objc_object {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
};

/// A pointer to an instance of a class.
typedef struct objc_object *id;
};
  1. isaobjc_object一個(gè)屬性,類(lèi)型是Class
  2. Class 是指向 objc_class的一個(gè)指針,存放著objc_class的地址
  3. id是一個(gè)objc_object結(jié)構(gòu)類(lèi)型的指針,這個(gè)類(lèi)型的對(duì)象能夠轉(zhuǎn)換成任何一種對(duì)象

Class

objc_class 是什么呢?讓我們看到<objc/runtime.h>

struct objc_class {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class _Nullable super_class                              OBJC2_UNAVAILABLE; // 父類(lèi) 
    const char * _Nonnull name                               OBJC2_UNAVAILABLE; // 類(lèi)名
    long version                                             OBJC2_UNAVAILABLE; //  類(lèi)的版本信息,默認(rèn)為0
    long info                                                OBJC2_UNAVAILABLE; // 類(lèi)信息,供運(yùn)行期間使用的一些位標(biāo)識(shí)
    long instance_size                                       OBJC2_UNAVAILABLE; // 該類(lèi)的實(shí)例變量大小
    struct objc_ivar_list * _Nullable ivars                  OBJC2_UNAVAILABLE; // 該類(lèi)的成員變量鏈表
    struct objc_method_list * _Nullable * _Nullable methodLists                    OBJC2_UNAVAILABLE; // 方法定義的鏈表
    struct objc_cache * _Nonnull cache                       OBJC2_UNAVAILABLE; // 方法緩存
    struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
  1. objc_class中也有一個(gè)isa指針,指向Meta Class。作用:因?yàn)?Objc 的類(lèi)本身也是一個(gè)對(duì)象,為了處理這個(gè)關(guān)系,runtime就創(chuàng)造了Meta Class,當(dāng)給類(lèi)發(fā)送 [NSObject alloc] 的消息時(shí),實(shí)際上是把這個(gè)消息發(fā)給 NSObject Class Object。
  2. cache 作用:對(duì)象接受到一個(gè)消息,首次會(huì)根據(jù)isa指針查找消息對(duì)象,然后在methodLists遍歷,調(diào)用后加入cache,下次直接從緩存獲取,提高調(diào)用效率。

objc_ivar_list

objc_ivar_list 結(jié)構(gòu)體存儲(chǔ)objc_ivar(成員變量)數(shù)組列表

struct objc_ivar {
    char * _Nullable ivar_name                               OBJC2_UNAVAILABLE; // 命名
    char * _Nullable ivar_type                               OBJC2_UNAVAILABLE; // 類(lèi)型
    int ivar_offset                                          OBJC2_UNAVAILABLE;
#ifdef __LP64__
    int space                                                OBJC2_UNAVAILABLE;
#endif
}    

struct objc_ivar_list {
    int ivar_count                                           OBJC2_UNAVAILABLE;
#ifdef __LP64__
    int space                                                OBJC2_UNAVAILABLE;
#endif
    /* variable length structure */
    struct objc_ivar ivar_list[1]                            OBJC2_UNAVAILABLE;
}                                                            OBJC2_UNAVAILABLE;

objc_method_list

objc_method_list結(jié)構(gòu)體存儲(chǔ)objc_method(方法)數(shù)組列表

struct objc_method {
    SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE;
    char * _Nullable method_types                            OBJC2_UNAVAILABLE;
    IMP _Nonnull method_imp                                  OBJC2_UNAVAILABLE;
}                                                            OBJC2_UNAVAILABLE;

struct objc_method_list {
    struct objc_method_list * _Nullable obsolete             OBJC2_UNAVAILABLE;

    int method_count                                         OBJC2_UNAVAILABLE;
#ifdef __LP64__
    int space                                                OBJC2_UNAVAILABLE;
#endif
    /* variable length structure */
    struct objc_method method_list[1]                        OBJC2_UNAVAILABLE;
}  

objc_cache

struct objc_cache {
    unsigned int mask /* total = mask + 1 */                 OBJC2_UNAVAILABLE;
    unsigned int occupied                                    OBJC2_UNAVAILABLE;
    Method _Nullable buckets[1]                              OBJC2_UNAVAILABLE;
};
  1. mask: 指定分配緩存bucket的總數(shù)。runtime 使用這個(gè)字段確定線(xiàn)性查找的索引位置
  2. occupied: 實(shí)際占用緩存 bucket的總數(shù)
  3. 指向Method數(shù)據(jù)結(jié)構(gòu)指針的數(shù)組,總數(shù)不能超過(guò)mask+1;指針可能為空,這標(biāo)識(shí)緩存bucket沒(méi)有被占用,數(shù)組隨著時(shí)間增長(zhǎng)

Meta Class & Super Class

提供一個(gè)實(shí)例,來(lái)體感metaclasssuperclass

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

typedef struct mtm_objc_class *mClass;
struct mtm_objc_class {
    mClass ISA;
    mClass superclass;
};

void testMetaClass(id self, SEL _cmd) {
    NSLog(@"This object is %p", self);
    NSLog(@"Class is %@, super class is %@", [self class], [self superclass]);
    
    Class currentClass = [self class];
    for (int i = 0; i < 4; i++) {
        NSLog(@"Following the [isa] pointer %d timer gives %p, class name is %@", i, currentClass, NSStringFromClass(currentClass));
        currentClass = object_getClass(currentClass);
    }
    
    mClass testClass = (__bridge mClass)[self superclass];
    for (int i = 0; i < 4; i++) {
        NSLog(@"Following the [super] pointer %d timer gives %p", i, testClass);
        testClass = testClass ? testClass->superclass : nil;
    }
    NSLog(@"NSObject's class is %p", [NSObject class]);
    Class meta_NSObject = object_getClass([NSObject class]);
    NSLog(@"NSobject's meta class is %p, meta super class is %p", meta_NSObject, ((__bridge mClass)meta_NSObject)->superclass);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        Class newClass = objc_allocateClassPair([NSURL class], "TestClass", 0);
        class_addMethod(newClass, @selector(testHandler), (IMP)testMetaClass, "v@:");
        objc_registerClassPair(newClass);
        id instance = [[newClass alloc] initWithString:@"http://www.baidu.com"];
        [instance performSelector:@selector(testHandler)];
    }
    return 0;
}

結(jié)果如下

ObjcTest[74791:1048449] This object is 0x100784c00
ObjcTest[74791:1048449] Class is TestClass, super class is NSURL
ObjcTest[74791:1048449] Following the [isa] pointer 0 timer gives 0x100784420, class name is TestClass
ObjcTest[74791:1048449] Following the [isa] pointer 1 timer gives 0x100784450, class name is TestClass
ObjcTest[74791:1048449] Following the [isa] pointer 2 timer gives 0x1003310f0, class name is NSObject
ObjcTest[74791:1048449] Following the [isa] pointer 3 timer gives 0x1003310f0, class name is NSObject
ObjcTest[74791:1048449] Following the [super] pointer 0 timer gives 0x7fff859d7230
ObjcTest[74791:1048449] Following the [super] pointer 1 timer gives 0x100331140
ObjcTest[74791:1048449] Following the [super] pointer 2 timer gives 0x0
ObjcTest[74791:1048449] Following the [super] pointer 3 timer gives 0x0
ObjcTest[74791:1048449] NSObject's class is 0x100331140
ObjcTest[74791:1048449] NSobject's meta class is 0x1003310f0, meta super class is 0x100331140

把日志中的地址放到圖里,應(yīng)該就更好理解 isa 鏈表super 鏈表

class_isa_super_class.png

小測(cè)試

來(lái)看看能答對(duì)幾個(gè)?

BOOL res1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];  
BOOL res2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];  
BOOL res3 = [(id)[NSURL class] isKindOfClass:[NSURL class]];  
BOOL res4 = [(id)[NSURL class] isMemberOfClass:[NSURL class]];

解密開(kāi)始

  1. 誤區(qū):isKindOfClass有類(lèi)方法和對(duì)象方法,實(shí)現(xiàn)有區(qū)別
  2. res1:第一次,Class 和 MetaClass(Class->ISA)對(duì)比,失敗;再跟NSObject's meta class 的 superclass 即 NSObject class,結(jié)果為true
  3. res2:Class->ISA == Class 為false,下面依此類(lèi)推
  4. res3 = false
  5. res4 = false
+ (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

- (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

+ (BOOL)isMemberOfClass:(Class)cls {
    return self->ISA() == cls;
}

- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文轉(zhuǎn)載自:http://southpeak.github.io/2014/10/25/objective-c-r...
    idiot_lin閱讀 1,047評(píng)論 0 4
  • Objective-C語(yǔ)言是一門(mén)動(dòng)態(tài)語(yǔ)言,它將很多靜態(tài)語(yǔ)言在編譯和鏈接時(shí)期做的事放到了運(yùn)行時(shí)來(lái)處理。這種動(dòng)態(tài)語(yǔ)言的...
    有一種再見(jiàn)叫青春閱讀 682評(píng)論 0 3
  • 我們常常會(huì)聽(tīng)說(shuō) Objective-C 是一門(mén)動(dòng)態(tài)語(yǔ)言,那么這個(gè)「動(dòng)態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,351評(píng)論 0 7
  • 本文詳細(xì)整理了 Cocoa 的 Runtime 系統(tǒng)的知識(shí),它使得 Objective-C 如虎添翼,具備了靈活的...
    lylaut閱讀 869評(píng)論 0 4
  • 本文轉(zhuǎn)載自:http://yulingtianxia.com/blog/2014/11/05/objective-...
    ant_flex閱讀 892評(píng)論 0 1

友情鏈接更多精彩內(nèi)容