Objective-C Runtime 運行時之四:Method Swizzling

一、引言

在本章中,我們來看看runtime對分類與協(xié)議的支持。

二、基礎(chǔ)數(shù)據(jù)類型

  • Category

Category是表示一個指向分類的結(jié)構(gòu)體的指針,其定義如下:

typedef struct objc_category *Category;

 

struct objc_category {

    char *category_name                          OBJC2_UNAVAILABLE; // 分類名

    char *class_name                             OBJC2_UNAVAILABLE; // 分類所屬的類名

    struct objc_method_list *instance_methods    OBJC2_UNAVAILABLE; // 實例方法列表

    struct objc_method_list *class_methods       OBJC2_UNAVAILABLE; // 類方法列表

    struct objc_protocol_list *protocols         OBJC2_UNAVAILABLE; // 分類所實現(xiàn)的協(xié)議列表

}

這個結(jié)構(gòu)體主要包含了分類定義的實例方法與類方法,其中instance_methods列表是objc_class中方法列表的一個子集,而class_methods列表是元類方法列表的一個子集。

  • Protocol

Protocol的定義如下:

typedef struct objc_object Protocol;

我們可以看到,Protocol其中實就是一個對象結(jié)構(gòu)體。

  • 操作函數(shù)

Runtime并沒有在<objc/runtime.h>頭文件中提供針對分類的操作函數(shù)。因為這些分類中的信息都包含在objc_class中,我們可以通過針對objc_class的操作函數(shù)來獲取分類的信息。如下例所示:

@interface RuntimeCategoryClass : NSObject

- (void)method1;

@end

 

@interface RuntimeCategoryClass (Category)

- (void)method2;

@end

 

@implementation RuntimeCategoryClass

 

- (void)method1 {

 

}

 

@end

 

@implementation RuntimeCategoryClass (Category)

 

- (void)method2 {

 

}

 

@end

 

#pragma mark -

 

NSLog(@"測試objc_class中的方法列表是否包含分類中的方法");

unsigned int outCount = 0;

Method *methodList = class_copyMethodList(RuntimeCategoryClass.class, &outCount);

 

for (int i = 0; i < outCount; i++) {

    Method method = methodList[i];

 

    const char *name = sel_getName(method_getName(method));

 

    NSLog(@"RuntimeCategoryClass's method: %s", name);

 

    if (strcmp(name, sel_getName(@selector(method2)))) {

        NSLog(@"分類方法method2在objc_class的方法列表中");

    }

}

其輸出是:

2014-11-08 10:36:39.213 [561:151847] 測試objc_class中的方法列表是否包含分類中的方法

2014-11-08 10:36:39.215 [561:151847] RuntimeCategoryClass's method: method2

2014-11-08 10:36:39.215 [561:151847] RuntimeCategoryClass's method: method1

2014-11-08 10:36:39.215 [561:151847] 分類方法method2在objc_class的方法列表中

而對于Protocol,runtime提供了一系列函數(shù)來對其進行操作,這些函數(shù)包括:

// 返回指定的協(xié)議

Protocol * objc_getProtocol ( const char *name );

 

// 獲取運行時所知道的所有協(xié)議的數(shù)組

Protocol ** objc_copyProtocolList ( unsigned int *outCount );

 

// 創(chuàng)建新的協(xié)議實例

Protocol * objc_allocateProtocol ( const char *name );

 

// 在運行時中注冊新創(chuàng)建的協(xié)議

void objc_registerProtocol ( Protocol *proto );

 

// 為協(xié)議添加方法

void protocol_addMethodDescription ( Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod );

 

// 添加一個已注冊的協(xié)議到協(xié)議中

void protocol_addProtocol ( Protocol *proto, Protocol *addition );

 

// 為協(xié)議添加屬性

void protocol_addProperty ( Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty );

 

// 返回協(xié)議名

const char * protocol_getName ( Protocol *p );

 

// 測試兩個協(xié)議是否相等

BOOL protocol_isEqual ( Protocol *proto, Protocol *other );

 

// 獲取協(xié)議中指定條件的方法的方法描述數(shù)組

struct objc_method_description * protocol_copyMethodDescriptionList ( Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount );

 

// 獲取協(xié)議中指定方法的方法描述

struct objc_method_description protocol_getMethodDescription ( Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod );

 

// 獲取協(xié)議中的屬性列表

objc_property_t * protocol_copyPropertyList ( Protocol *proto, unsigned int *outCount );

 

// 獲取協(xié)議的指定屬性

objc_property_t protocol_getProperty ( Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty );

 

// 獲取協(xié)議采用的協(xié)議

Protocol ** protocol_copyProtocolList ( Protocol *proto, unsigned int *outCount );

 

// 查看協(xié)議是否采用了另一個協(xié)議

BOOL protocol_conformsToProtocol ( Protocol *proto, Protocol *other );

注意:

  • objc_getProtocol函數(shù),需要注意的是如果僅僅是聲明了一個協(xié)議,而未在任何類中實現(xiàn)這個協(xié)議,則該函數(shù)返回的是nil。
  • objc_copyProtocolList函數(shù),獲取到的數(shù)組需要使用free來釋放
  • objc_allocateProtocol函數(shù),如果同名的協(xié)議已經(jīng)存在,則返回nil
  • objc_registerProtocol函數(shù),創(chuàng)建一個新的協(xié)議后,必須調(diào)用該函數(shù)以在運行時中注冊新的協(xié)議。協(xié)議注冊后便可以使用,但不能再做修改,即注冊完后不能再向協(xié)議添加方法或協(xié)議

需要強調(diào)的是,協(xié)議一旦注冊后就不可再修改,即無法再通過調(diào)用protocol_addMethodDescription、protocol_addProtocol和protocol_addProperty往協(xié)議中添加方法等。

三、總結(jié)

Runtime并沒有提供過多的函數(shù)來處理分類。對于協(xié)議,我們可以動態(tài)地創(chuàng)建協(xié)議,并向其添加方法、屬性及繼承的協(xié)議,并在運行時動態(tài)地獲取這些信息。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容