概述
Objc Runtime使得C具有了面向對象能力,在程序運行時創建,檢查,修改類、對象和它們的方法。Runtime是C和彙編編寫的,這裏http://www.opensource.apple.com/source/objc4/可以下到蘋果維護的開源代碼,GNU也有一個開源的runtime版本,他們都努力的保持一致。蘋果官方的Runtime編程指南
Runtime函數
Runtime系統是由一系列的函數和數據結構組成的公共接口動態共享庫,在/usr/include/objc目錄下可以看到頭文件,可以用其中一些函數通過C語言實現Objective-C中一樣的功能。蘋果官方文檔https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html裏有詳細的Runtime函數文檔。
Class和Object基礎數據結構
Class
objc/runtime.h中objc_class結構體的定義如下:
struct objc_class { Class isa OBJC_ISA_AVAILABILITY; //isa指針指向Meta Class,因爲Objc的類的本身也是一個Object,爲了處理這個關係,runtime就創造了Meta Class,當給類發送[NSObject alloc]這樣消息時,實際上是把這個消息發給了Class Object #if !__OBJC2__ Class super_class OBJC2_UNAVAILABLE; // 父類 const char *name OBJC2_UNAVAILABLE; // 類名 long version OBJC2_UNAVAILABLE; // 類的版本信息,默認爲0 long info OBJC2_UNAVAILABLE; // 類信息,供運行期使用的一些位標識 long instance_size OBJC2_UNAVAILABLE; // 該類的實例變量大小 struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; // 該類的成員變量鏈表 struct objc_method_list **methodLists OBJC2_UNAVAILABLE; // 方法定義的鏈表 struct objc_cache *cache OBJC2_UNAVAILABLE; // 方法緩存,對象接到一個消息會根據isa指針查找消息對象,這時會在methodLists中遍歷,如果cache了,常用的方法調用時就能夠提高調用的效率。 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 協議鏈表 #endif } OBJC2_UNAVAILABLE;
objc_ivar_list和objc_method_list的定義
//objc_ivar_list結構體存儲objc_ivar數組列表 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的數組列表 struct objc_method_list { struct objc_method_list *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_object與id
objc_object是一個類的實例結構體,objc/objc.h中objc_object是一個類的實例結構體定義如下:
struct objc_object { Class isa OBJC_ISA_AVAILABILITY; }; typedef struct objc_object *id;
向object發送消息時,Runtime庫會根據object的isa指針找到這個實例object所屬於的類,然後在類的方法列表以及父類方法列表尋找對應的方法運行。id是一個objc_object結構類型的指針,這個類型的對象能夠轉換成任何一種對象。
objc_cache
objc_class結構體中的cache字段用於緩存調用過的method。cache指針指向objc_cache結構體,這個結構體的定義如下
struct objc_cache { unsigned int mask /* total = mask + 1 */ OBJC2_UNAVAILABLE; //指定分配緩存bucket的總數。runtime使用這個字段確定線性查找數組的索引位置 unsigned int occupied OBJC2_UNAVAILABLE; //實際佔用緩存bucket總數 Method buckets[1] OBJC2_UNAVAILABLE; //指向Method數據結構指針的數組,這個數組的總數不能超過mask+1,但是指針是可能爲空的,這就表示緩存bucket沒有被佔用,數組會隨着時間增長。 };
Meta Class
meta class是一個類對象的類,當向對象發消息,runtime會在這個對象所屬類方法列表中查找發送消息對應的方法,但當向類發送消息時,runtime就會在這個類的meta class方法列表裏查找。所有的meta class,包括Root class,Superclass,Subclass的isa都指向Root class的meta class,這樣能夠形成一個閉環。
void TestMetaClass(id self, SEL _cmd) { NSLog(@"This objcet 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 times gives %p", i, currentClass); //通過objc_getClass獲得對象isa,這樣可以回溯到Root class及NSObject的meta class,可以看到最後指針指向的是0x0和NSObject的meta class類地址一樣。 currentClass = objc_getClass((__bridge void *)currentClass); } NSLog(@"NSObject's class is %p", [NSObject class]); NSLog(@"NSObject's meta class is %p", objc_getClass((__bridge void *)[NSObject class])); } @implementation Test - (void)ex_registerClassPair { Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0); class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:"); objc_registerClassPair(newClass); id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil]; [instance performSelector:@selector(testMetaClass)]; } @end
運行結果
2014-10-20 22:57:07.352 mountain[1303:41490] This objcet is 0x7a6e22b0 2014-10-20 22:57:07.353 mountain[1303:41490] Class is TestStringClass, super class is NSError 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 0 times gives 0x7a6e21b0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 1 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 2 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 3 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] NSObject's class is 0xe10000 2014-10-20 22:57:07.354 mountain[1303:41490] NSObject's meta class is 0x0
舉個例子
@interface Sark : NSObject @end @implementation Sark @end int main(int argc, const char * argv[]) { @autoreleasepool { BOOL res1 = [(id)[NSObject class] isKindOfClass:[NSObject class]]; BOOL res2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]]; BOOL res3 = [(id)[Sark class] isKindOfClass:[Sark class]]; BOOL res4 = [(id)[Sark class] isMemberOfClass:[Sark class]]; NSLog(@"%d %d %d %d", res1, res2, res3, res4); } return 0; } //輸出 2014-11-05 14:45:08.474 Test[9412:721945] 1 0 0 0
先看看isKindOfClass和isMemberOfClass在Object.mm中的實現
- (BOOL)isKindOf:aClass { Class cls; for (cls = isa; cls; cls = cls->superclass) if (cls == (Class)aClass) return YES; return NO; } - (BOOL)isMemberOf:aClass { return isa == (Class)aClass; }
res1中,可以從isKindOfClass看出NSObject class的isa第一次會指向NSObject的Meta Class,接着Super class時會NSObject的Meta Class根據前面講的閉環可以知道是會指到NSObject class,這樣res1的bool值就是真了。
res2的話因爲是isMemberOf就只有一次,那麼是NSObject的Meta Class和NSObject class不同返回的bool值就是false了。
res3第一次是Sark Meta Class,第二次super class 後就是NSObject Meta Class了,返回也是false
res4是Sark Meta Class,所以返回也是false
類與對象操作函數
runtime有很多的函數可以操作類和對象。類相關的是class爲前綴,對象相關操作是objc或object_爲前綴。
類相關操作函數
name
// 獲取類的類名 const char * class_getName ( Class cls );
super_class和meta-class
// 獲取類的父類 Class class_getSuperclass ( Class cls ); // 判斷給定的Class是否是一個meta class BOOL class_isMetaClass ( Class cls );
instance_size
// 獲取實例大小 size_t class_getInstanceSize ( Class cls );
成員變量(ivars)及屬性
//成員變量操作函數 // 獲取類中指定名稱實例成員變量的信息 Ivar class_getInstanceVariable ( Class cls, const char *name ); // 獲取類成員變量的信息 Ivar class_getClassVariable ( Class cls, const char *name ); // 添加成員變量 BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types ); //這個只能夠向在runtime時創建的類添加成員變量 // 獲取整個成員變量列表 Ivar * class_copyIvarList ( Class cls, unsigned int *outCount ); //必須使用free()來釋放這個數組 //屬性操作函數 // 獲取類中指定名稱實例成員變量的信息 Ivar class_getInstanceVariable ( Class cls, const char *name ); // 獲取類成員變量的信息 Ivar class_getClassVariable ( Class cls, const char *name ); // 添加成員變量 BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types ); // 獲取整個成員變量列表 Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );
methodLists
// 添加方法 BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types ); //和成員變量不同的是可以爲類動態添加方法。如果有同名會返回NO,修改的話需要使用method_setImplementation // 獲取實例方法 Method class_getInstanceMethod ( Class cls, SEL name ); // 獲取類方法 Method class_getClassMethod ( Class cls, SEL name ); // 獲取所有方法的數組 Method * class_copyMethodList ( Class cls, unsigned int *outCount ); // 替代方法的實現 IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types ); // 返回方法的具體實現 IMP class_getMethodImplementation ( Class cls, SEL name ); IMP class_getMethodImplementation_stret ( Class cls, SEL name ); // 類實例是否響應指定的selector BOOL class_respondsToSelector ( Class cls, SEL sel );
objc_protocol_list
// 添加協議 BOOL class_addProtocol ( Class cls, Protocol *protocol ); // 返回類是否實現指定的協議 BOOL class_conformsToProtocol ( Class cls, Protocol *protocol ); // 返回類實現的協議列表 Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount );
version
// 獲取版本號 int class_getVersion ( Class cls ); // 設置版本號 void class_setVersion ( Class cls, int version );
實例
通過實例來消化下上面的那些函數
//----------------------------------------------------------- // MyClass.h @interface MyClass : NSObject <NSCopying, NSCoding> @property (nonatomic, strong) NSArray *array; @property (nonatomic, copy) NSString *string; - (void)method1; - (void)method2; + (void)classMethod1; @end //----------------------------------------------------------- // MyClass.m #import "MyClass.h" @interface MyClass () { NSInteger _instance1; NSString * _instance2; } @property (nonatomic, assign) NSUInteger integer; - (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2; @end @implementation MyClass + (void)classMethod1 { } - (void)method1 { NSLog(@"call method method1"); } - (void)method2 { } - (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2 { NSLog(@"arg1 : %ld, arg2 : %@", arg1, arg2); } @end //----------------------------------------------------------- // main.h #import "MyClass.h" #import "MySubClass.h" #import <objc/runtime.h> int main(int argc, const char * argv[]) { @autoreleasepool { MyClass *myClass = [[MyClass alloc] init]; unsigned int outCount = 0; Class cls = myClass.class; // 類名 NSLog(@"class name: %s", class_getName(cls)); NSLog(@"=========================================================="); // 父類 NSLog(@"super class name: %s", class_getName(class_getSuperclass(cls))); NSLog(@"=========================================================="); // 是否是元類 NSLog(@"MyClass is %@ a meta-class", (class_isMetaClass(cls) ? @"" : @"not")); NSLog(@"=========================================================="); Class meta_class = objc_getMetaClass(class_getName(cls)); NSLog(@"%s's meta-class is %s", class_getName(cls), class_getName(meta_class)); NSLog(@"=========================================================="); // 變量實例大小 NSLog(@"instance size: %zu", class_getInstanceSize(cls)); NSLog(@"=========================================================="); // 成員變量 Ivar *ivars = class_copyIvarList(cls, &outCount); for (int i = 0; i < outCount; i++) { Ivar ivar = ivars[i]; NSLog(@"instance variable's name: %s at index: %d", ivar_getName(ivar), i); } free(ivars); Ivar string = class_getInstanceVariable(cls, "_string"); if (string != NULL) { NSLog(@"instace variable %s", ivar_getName(string)); } NSLog(@"=========================================================="); // 屬性操作 objc_property_t * properties = class_copyPropertyList(cls, &outCount); for (int i = 0; i < outCount; i++) { objc_property_t property = properties[i]; NSLog(@"property's name: %s", property_getName(property)); } free(properties); objc_property_t array = class_getProperty(cls, "array"); if (array != NULL) { NSLog(@"property %s", property_getName(array)); } NSLog(@"=========================================================="); // 方法操作 Method *methods = class_copyMethodList(cls, &outCount); for (int i = 0; i < outCount; i++) { Method method = methods[i]; NSLog(@"method's signature: %s", method_getName(method)); } free(methods); Method method1 = class_getInstanceMethod(cls, @selector(method1)); if (method1 != NULL) { NSLog(@"method %s", method_getName(method1)); } Method classMethod = class_getClassMethod(cls, @selector(classMethod1)); if (classMethod != NULL) { NSLog(@"class method : %s", method_getName(classMethod)); } NSLog(@"MyClass is%@ responsd to selector: method3WithArg1:arg2:", class_respondsToSelector(cls, @selector(method3WithArg1:arg2:)) ? @"" : @" not"); IMP imp = class_getMethodImplementation(cls, @selector(method1)); imp(); NSLog(@"=========================================================="); // 協議 Protocol * __unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount); Protocol * protocol; for (int i = 0; i < outCount; i++) { protocol = protocols[i]; NSLog(@"protocol name: %s", protocol_getName(protocol)); } NSLog(@"MyClass is%@ responsed to protocol %s", class_conformsToProtocol(cls, protocol) ? @"" : @" not", protocol_getName(protocol)); NSLog(@"=========================================================="); } return 0; }
輸出結果
2014-10-22 19:41:37.452 RuntimeTest[3189:156810] class name: MyClass 2014-10-22 19:41:37.453 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] super class name: NSObject 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass is not a meta-class 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass's meta-class is MyClass 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance size: 48 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance1 at index: 0 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance2 at index: 1 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _array at index: 2 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _string at index: 3 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instance variable's name: _integer at index: 4 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instace variable _string 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: array 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: string 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property's name: integer 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property array 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method1 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method2 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method3WithArg1:arg2: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: integer 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setInteger: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: array 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: string 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setString: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setArray: 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method's signature: .cxx_destruct 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method method1 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] class method : classMethod1 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] MyClass is responsd to selector: method3WithArg1:arg2: 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] call method method1 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCopying 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCoding 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] MyClass is responsed to protocol NSCoding 2014-10-22 19:41:37.468 RuntimeTest[3189:156810] ======================================
動態創建類和對象
動態創建類
// 創建一個新類和元類 Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes ); //如果創建的是root class,則superclass爲Nil。extraBytes通常爲0 // 銷燬一個類及其相關聯的類 void objc_disposeClassPair ( Class cls ); //在運行中還存在或存在子類實例,就不能夠調用這個。 // 在應用中註冊由objc_allocateClassPair創建的類 void objc_registerClassPair ( Class cls ); //創建了新類後,然後使用class_addMethod,class_addIvar函數爲新類添加方法,實例變量和屬性後再調用這個來註冊類,再之後就能夠用了。
使用實例
Class cls = objc_allocateClassPair(MyClass.class, "MySubClass", 0); class_addMethod(cls, @selector(submethod1), (IMP)imp_submethod1, "v@:"); class_replaceMethod(cls, @selector(method1), (IMP)imp_submethod1, "v@:"); class_addIvar(cls, "_ivar1", sizeof(NSString *), log(sizeof(NSString *)), "i"); objc_property_attribute_t type = {"T", "@"NSString""}; objc_property_attribute_t ownership = { "C", "" }; objc_property_attribute_t backingivar = { "V", "_ivar1"}; objc_property_attribute_t attrs[] = {type, ownership, backingivar}; class_addProperty(cls, "property2", attrs, 3); objc_registerClassPair(cls); id instance = [[cls alloc] init]; [instance performSelector:@selector(submethod1)]; [instance performSelector:@selector(method1)];
輸出
2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1 2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1
動態創建對象
// 創建類實例 id class_createInstance ( Class cls, size_t extraBytes ); //會在heap裏給類分配內存。這個方法和+alloc方法類似。 // 在指定位置創建類實例 id objc_constructInstance ( Class cls, void *bytes ); // 銷燬類實例 void * objc_destructInstance ( id obj ); //不會釋放移除任何相關引用
測試下效果
//可以看出class_createInstance和alloc的不同 id theObject = class_createInstance(NSString.class, sizeof(unsigned)); id str1 = [theObject init]; NSLog(@"%@", [str1 class]); id str2 = [[NSString alloc] initWithString:@"test"]; NSLog(@"%@", [str2 class]);
輸出結果
2014-10-23 12:46:50.781 RuntimeTest[4039:89088] NSString 2014-10-23 12:46:50.781 RuntimeTest[4039:89088] __NSCFConstantString
實例操作函數
這些函數是針對創建的實例對象的一系列操作函數。
整個對象操作的函數
// 返回指定對象的一份拷貝 id object_copy ( id obj, size_t size ); // 釋放指定對象佔用的內存 id object_dispose ( id obj );
應用場景
//把a轉換成佔用更多空間的子類b NSObject *a = [[NSObject alloc] init]; id newB = object_copy(a, class_getInstanceSize(MyClass.class)); object_setClass(newB, MyClass.class); object_dispose(a);
對象實例變量進行操作的函數
// 修改類實例的實例變量的值 Ivar object_setInstanceVariable ( id obj, const char *name, void *value ); // 獲取對象實例變量的值 Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue ); // 返回指向給定對象分配的任何額外字節的指針 void * object_getIndexedIvars ( id obj ); // 返回對象中實例變量的值 id object_getIvar ( id obj, Ivar ivar ); // 設置對象中實例變量的值 void object_setIvar ( id obj, Ivar ivar, id value );
對對象類操作
// 返回給定對象的類名 const char * object_getClassName ( id obj ); // 返回對象的類 Class object_getClass ( id obj ); // 設置對象的類 Class object_setClass ( id obj, Class cls );
獲取類定義
// 獲取已註冊的類定義的列表 int objc_getClassList ( Class *buffer, int bufferCount ); // 創建並返回一個指向所有已註冊類的指針列表 Class * objc_copyClassList ( unsigned int *outCount ); // 返回指定類的類定義 Class objc_lookUpClass ( const char *name ); Class objc_getClass ( const char *name ); Class objc_getRequiredClass ( const char *name ); // 返回指定類的元類 Class objc_getMetaClass ( const char *name );
演示如何使用
int numClasses; Class * classes = NULL; numClasses = objc_getClassList(NULL, 0); if (numClasses > 0) { classes = malloc(sizeof(Class) * numClasses); numClasses = objc_getClassList(classes, numClasses); NSLog(@"number of classes: %d", numClasses); for (int i = 0; i < numClasses; i++) { Class cls = classes[i]; NSLog(@"class name: %s", class_getName(cls)); } free(classes); }
結果如下:
2014-10-23 16:20:52.589 RuntimeTest[8437:188589] number of classes: 1282 2014-10-23 16:20:52.589 RuntimeTest[8437:188589] class name: DDTokenRegexp 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: _NSMostCommonKoreanCharsKeySet 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: OS_xpc_dictionary 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: NSFileCoordinator 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: NSAssertionHandler 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: PFUbiquityTransactionLogMigrator 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: NSNotification 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: NSKeyValueNilSetEnumerator 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: OS_tcp_connection_tls_session 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: _PFRoutines ......還有大量輸出
成員變量與屬性
基礎數據類型
Ivar
實例變量類型,指向objc_ivar結構體的指針,ivar指針地址是根據class結構體的地址加上基地址偏移字節得到的。
typedef struct objc_ivar *Ivar; struct objc_ivar { char *ivar_name OBJC2_UNAVAILABLE; // 變量名 char *ivar_type OBJC2_UNAVAILABLE; // 變量類型 int ivar_offset OBJC2_UNAVAILABLE; // 基地址偏移字節 #ifdef __LP64__ int space OBJC2_UNAVAILABLE; #endif }
objc_property_t
屬性類型,指向objc_property結構體
typedef struct objc_property *objc_property_t;
通過class_copyPropertyList和protocol_copyPropertyList方法獲取類和協議的屬性
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount) objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
示例
@interface Lender : NSObject { float alone; } @property float alone; @end //獲取屬性列表 id LenderClass = objc_getClass("Lender"); unsigned int outCount; objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount); //查找屬性名稱 const char *property_getName(objc_property_t property) //通過給出的名稱來在類和協議中獲取屬性的引用 objc_property_t class_getProperty(Class cls, const char *name) objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty) //發掘屬性名稱和@encode類型字符串 const char *property_getAttributes(objc_property_t property) //從一個類中獲取它的屬性 id LenderClass = objc_getClass("Lender"); unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount); for (i = 0; i < outCount; i++) { objc_property_t property = properties[i]; fprintf(stdout, "%s %sn", property_getName(property), property_getAttributes(property)); }
objc_property_attribute_t
也是結構體,定義屬性的attribute
typedef struct { const char *name; // 特性名 const char *value; // 特性值 } objc_property_attribute_t;
示例
下面代碼會編譯出錯,Runtime Crash還是正常輸出
@interface Sark : NSObject @property (nonatomic, copy) NSString *name; @end @implementation Sark - (void)speak { NSLog(@"my name is %@", self.name); } @end @interface Test : NSObject @end @implementation Test - (instancetype)init { self = [super init]; if (self) { id cls = [Sark class]; void *obj = &cls; [(__bridge id)obj speak]; } return self; } @end int main(int argc, const char * argv[]) { @autoreleasepool { [[Test alloc] init]; } return 0; } //結果正常輸出 2014-11-07 14:08:25.698 Test[1097:57255] my name is
obj爲指向Sark Class的指針,相當於Sark的實例對象但是還是不一樣,根據objc_msgSend流程,obj指針能夠在方法列表中找到speak方法,所以運行正常。
爲了得到self.name能夠輸出的原因,可以加入調試代碼
- (void)speak { unsigned int numberOfIvars = 0; Ivar *ivars = class_copyIvarList([self class], &numberOfIvars); for(const Ivar *p = ivars; p < ivars+numberOfIvars; p++) { Ivar const ivar = *p; ptrdiff_t offset = ivar_getOffset(ivar); const char *name = ivar_getName(ivar); NSLog(@"Sark ivar name = %s, offset = %td", name, offset); } NSLog(@"my name is %p", &_name); NSLog(@"my name is %@", *(&_name)); } @implementation Test - (instancetype)init { self = [super init]; if (self) { NSLog(@"Test instance = %@", self); void *self2 = (__bridge void *)self; NSLog(@"Test instance pointer = %p", &self2); id cls = [Sark class]; NSLog(@"Class instance address = %p", cls); void *obj = &cls; NSLog(@"Void *obj = %@", obj); [(__bridge id)obj speak]; } return self; } @end //輸出 2014-11-11 00:56:02.464 Test[10475:1071029] Test instance = 2014-11-11 00:56:02.464 Test[10475:1071029] Test instance pointer = 0x7fff5fbff7c8 2014-11-11 00:56:02.465 Test[10475:1071029] Class instance address = 0x1000023c8 2014-11-11 00:56:02.465 Test[10475:1071029] Void *obj = 2014-11-11 00:56:02.465 Test[10475:1071029] Sark ivar name = _name, offset = 8 2014-11-11 00:56:02.465 Test[10475:1071029] my name is 0x7fff5fbff7c8 2014-11-11 00:56:02.465 Test[10475:1071029] my name is
Sark中Propertyname會被轉換成ivar到類的結構裏,runtime會計算ivar的地址偏移來找ivar的最終地址,根據輸出可以看出Sark class的指針地址加上ivar的偏移量正好跟Test對象指針地址。
關聯對象
關聯對象是在運行時添加的類似成員。
//將一個對象連接到其它對象 static char myKey; objc_setAssociatedObject(self, &myKey, anObject, OBJC_ASSOCIATION_RETAIN); //獲取一個新的關聯的對象 id anObject = objc_getAssociatedObject(self, &myKey); //使用objc_removeAssociatedObjects函數移除一個關聯對象
實例演示關聯對象使用
//動態的將一個Tap手勢操作連接到任何UIView中。 - (void)setTapActionWithBlock:(void (^)(void))block { UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerTapGestureKey); if (!gesture) { gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)]; [self addGestureRecognizer:gesture]; //將創建的手勢對象和block作爲關聯對象 objc_setAssociatedObject(self, &kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY); } //手勢識別對象的target和action - (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { void(^action)(void) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey); if (action) { action(); } } }
成員變量和屬性的操作方法
成員變量
// 獲取成員變量名 const char * ivar_getName ( Ivar v ); // 獲取成員變量類型編碼 const char * ivar_getTypeEncoding ( Ivar v ); // 獲取成員變量的偏移量 ptrdiff_t ivar_getOffset ( Ivar v );
Associated Objects
// 設置關聯對象 void objc_setAssociatedObject ( id object, const void *key, id value, objc_AssociationPolicy policy ); // 獲取關聯對象 id objc_getAssociatedObject ( id object, const void *key ); // 移除關聯對象 void objc_removeAssociatedObjects ( id object ); //上面方法以鍵值對的形式動態的向對象添加,獲取或者刪除關聯值。其中關聯政策是一組枚舉常量。這些常量對應着引用關聯值機制,也就是Objc內存管理的引用計數機制。 enum { OBJC_ASSOCIATION_ASSIGN = 0, OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, OBJC_ASSOCIATION_COPY_NONATOMIC = 3, OBJC_ASSOCIATION_RETAIN = 01401, OBJC_ASSOCIATION_COPY = 01403 };
屬性
// 獲取屬性名 const char * property_getName ( objc_property_t property ); // 獲取屬性特性描述字符串 const char * property_getAttributes ( objc_property_t property ); // 獲取屬性中指定的特性 char * property_copyAttributeValue ( objc_property_t property, const char *attributeName ); // 獲取屬性的特性列表 objc_property_attribute_t * property_copyAttributeList ( objc_property_t property, unsigned int *outCount );
實例
兩個接口同樣數據不同的字段名處理
@interface MyObject: NSObject @property (nonatomic, copy) NSString * name; @property (nonatomic, copy) NSString * status; @end //返回字典數據有不同的字段名,一般是寫兩個方法,但是如果靈活用runtime只用寫一個方法 @{@"name1": "張三", @"status1": @"start"} @{@"name2": "張三", @"status2": @"end"} //定義一個映射字典(全局) static NSMutableDictionary *map = nil; @implementation MyObject + (void)load { map = [NSMutableDictionary dictionary]; map[@"name1"] = @"name"; map[@"status1"] = @"status"; map[@"name2"] = @"name"; map[@"status2"] = @"status"; } @end //不同字段映射到MyObject相同屬性上 - (void)setDataWithDic:(NSDictionary *)dic { [dic enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { NSString *propertyKey = [self propertyForKey:key]; if (propertyKey) { objc_property_t property = class_getProperty([self class], [propertyKey UTF8String]); // TODO: 針對特殊數據類型做處理 NSString *attributeString = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; ... [self setValue:obj forKey:propertyKey]; } }]; }
Method和消息
Method和消息的基礎數據類型
SEL
選擇器表示一個方法的selector的指針,可以理解爲Method中的ID類型
typedef struct objc_selector *SEL; //objc_selector編譯時會根據每個方法名字參數序列生成唯一標識 SEL sel1 = @selector(method1); NSLog(@"sel : %p", sel1); 輸出 2014-10-30 18:40:07.518 RuntimeTest[52734:466626] sel : 0x100002d72
獲取SEL的三個方法:
- sel_registerName函數
- Objective-C編譯器提供的@selector()
- NSSelectorFromString()方法
IMP
是函數指針,指向方法的首地址,通過SEL快速得到對應IMP,這時可以跳過Runtime消息傳遞機制直接執行函數,比直接向對象發消息高效。定義如下
id (*IMP)(id, SEL, ...)
Method
用於表示類定義中的方法
typedef struct objc_method *Method; struct objc_method { SEL method_name OBJC2_UNAVAILABLE; // 方法名 char *method_types OBJC2_UNAVAILABLE; //是個char指針,存儲着方法的參數類型和返回值類型 IMP method_imp OBJC2_UNAVAILABLE; // 方法實現,函數指針 }
Method相關操作函數
Method
// 調用指定方法的實現,返回的是方法實現時的返回,參數receiver不能爲空,這個比method_getImplementation和method_getName快 id method_invoke ( id receiver, Method m, ... ); // 調用返回一個數據結構的方法的實現 void method_invoke_stret ( id receiver, Method m, ... ); // 獲取方法名,希望獲得方法明的C字符串,使用sel_getName(method_getName(method)) SEL method_getName ( Method m ); // 返回方法的實現 IMP method_getImplementation ( Method m ); // 獲取描述方法參數和返回值類型的字符串 const char * method_getTypeEncoding ( Method m ); // 獲取方法的返回值類型的字符串 char * method_copyReturnType ( Method m ); // 獲取方法的指定位置參數的類型字符串 char * method_copyArgumentType ( Method m, unsigned int index ); // 通過引用返回方法的返回值類型字符串 void method_getReturnType ( Method m, char *dst, size_t dst_len ); // 返回方法的參數的個數 unsigned int method_getNumberOfArguments ( Method m ); // 通過引用返回方法指定位置參數的類型字符串 void method_getArgumentType ( Method m, unsigned int index, char *dst, size_t dst_len ); // 返回指定方法的方法描述結構體 struct objc_method_description * method_getDescription ( Method m ); // 設置方法的實現 IMP method_setImplementation ( Method m, IMP imp ); // 交換兩個方法的實現 void method_exchangeImplementations ( Method m1, Method m2 );
Method的SEL
// 返回給定選擇器指定的方法的名稱 const char * sel_getName ( SEL sel ); // 在Objective-C Runtime系統中註冊一個方法,將方法名映射到一個選擇器,並返回這個選擇器 SEL sel_registerName ( const char *str ); // 在Objective-C Runtime系統中註冊一個方法 SEL sel_getUid ( const char *str ); // 比較兩個選擇器 BOOL sel_isEqual ( SEL lhs, SEL rhs );
Method調用流程
消息函數,Objc中發送消息是用中括號把接收者和消息括起來,只到運行時纔會把消息和方法實現綁定。
//這個函數將消息接收者和方法名作爲基礎參數。消息發送給一個對象時,objc_msgSend通過對象的isa指針獲得類的結構體,先在Cache裏找,找到就執行,沒找到就在分發列表裏查找方法的selector,沒找到就通過objc_msgSend結構體中指向父類的指針找到父類,然後在父類分發列表找,直到root class(NSObject)。 objc_msgSend(receiver, selector, arg1, arg2, ...)
編譯器會根據情況在objc_msgSend,objc_msgSend_stret,objc_msgSendSuper,或objc_msgSendSuper_stret四個方法中選一個調用。如果是傳遞給超類就會調用帶super的函數,如果返回是數據結構而不是一個值就會調用帶stret的函數。在i386平臺返回類型爲浮點消息會調用objc_msgSend_fpret函數。
舉個例子,NSStringFromClass([self class])和NSStringFromClass([super class])輸出都是self的類名。原因如下
調用[self class]的時候先調用objc_msgSend,發現self沒有class這個方法,然後用objc_msgSendSuper就去父類找,還是沒有,繼續用objc_msgSendSuper到NSObject裏找,結果找到了,查找NSObject中class方法的runtime源碼會發現它會返回self,所以[self class]會返回self本身。同理[super class]相對前者就是少了objc_msgSend這一步,最後也會找到NSObject根類裏的class方法,自然結果也是返回了self。
Method中的接收消息對象參數和方法選擇器參數
在Method中使用self關鍵字來引用實例本身,self的內容即接收消息的對象是在Method運行時被傳入的同時還有方法選擇器。
獲取Method地址
使用NSObject提供的methodForSelector:方法可以獲得Method的指針,通過指針調用實現代碼。
void (*setter)(id, SEL, BOOL); int i; setter = (void (*)(id, SEL, BOOL))[target methodForSelector:@selector(setFilled:)]; for ( i = 0 ; i < 1000 ; i++ ) setter(targetList[i], @selector(setFilled:), YES);
Method轉發
如果使用[object message]調用方法,object無法響應message時就會報錯。用performSelector...調用就要等到運行時才確定是否能接受,不能才崩潰。
//先調用respondsToSelector:來判斷一下 if ([self respondsToSelector:@selector(method)]) { [self performSelector:@selector(method)]; }
Method轉發機制分爲三步:
動態方法解析
void functionForMethod1(id self, SEL _cmd) { NSLog(@"%@, %p", self, _cmd); } + (BOOL)resolveInstanceMethod:(SEL)sel { NSString *selectorString = NSStringFromSelector(sel); if ([selectorString isEqualToString:@"method1"]) { class_addMethod(self.class, @selector(method1), (IMP)functionForMethod1, "@:"); } return [super resolveInstanceMethod:sel]; }
可以動態的提供一個方法的實現。例如可以用@dynamic關鍵字在類的實現文件中寫個屬性
//這個表明會爲這個屬性動態提供set get方法,就是編譯器是不會默認生成setPropertyName:和propertyName方法,需要動態提供。可以通過重載resolveInstanceMethod:和resolveClassMethod:方法分別添加實例方法和類方法實現。最後用class_addMethod完成添加特定方法實現的操作 @dynamic propertyName; // void dynamicMethodIMP(id self, SEL _cmd) { // implementation .... } @implementation MyClass + (BOOL)resolveInstanceMethod:(SEL)aSEL { if (aSEL == @selector(resolveThisMethodDynamically)) { //v@:表示返回值和參數,可以在蘋果官網查看Type Encoding相關文檔 https://developer.apple.com/library/mac/DOCUMENTATION/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:"); return YES; } return [super resolveInstanceMethod:aSEL]; } @end
重定向接收者
如果無法處理消息會繼續調用下面的方法,同時在這裏Runtime系統實際上是給了一個替換消息接收者的機會,但是替換的對象千萬不要是self,那樣會進入死循環。
- (id)forwardingTargetForSelector:(SEL)aSelector { if(aSelector == @selector(mysteriousMethod:)){ return alternateObject; } return [super forwardingTargetForSelector:aSelector]; }
使用這個方法通常在對象內部,如下
@interface SUTRuntimeMethodHelper : NSObject - (void)method2; @end @implementation SUTRuntimeMethodHelper - (void)method2 { NSLog(@"%@, %p", self, _cmd); } @end #pragma mark - @interface SUTRuntimeMethod () { SUTRuntimeMethodHelper *_helper; } @end @implementation SUTRuntimeMethod + (instancetype)object { return [[self alloc] init]; } - (instancetype)init { self = [super init]; if (self != nil) { _helper = [[SUTRuntimeMethodHelper alloc] init]; } return self; } - (void)test { [self performSelector:@selector(method2)]; } - (id)forwardingTargetForSelector:(SEL)aSelector { NSLog(@"forwardingTargetForSelector"); NSString *selectorString = NSStringFromSelector(aSelector); // 將消息轉發給_helper來處理 if ([selectorString isEqualToString:@"method2"]) { return _helper; } return [super forwardingTargetForSelector:aSelector]; } @end
最後進行轉發
如果以上兩種都沒法處理未知消息就需要完整消息轉發了。調用如下方法
//這一步是最後機會將消息轉發給其它對象,對象會將未處理的消息相關的selector,target和參數都封裝在anInvocation中。forwardInvocation:像未知消息分發中心,將未知消息轉發給其它對象。注意的是forwardInvocation:方法只有在消息接收對象無法正常響應消息時才被調用。 - (void)forwardInvocation:(NSInvocation *)anInvocation //必須重寫這個方法,消息轉發使用這個方法獲得的信息創建NSInvocation對象。 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
範例
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *signature = [super methodSignatureForSelector:aSelector]; if (!signature) { if ([SUTRuntimeMethodHelper instancesRespondToSelector:aSelector]) { signature = [SUTRuntimeMethodHelper instanceMethodSignatureForSelector:aSelector]; } } return signature; } - (void)forwardInvocation:(NSInvocation *)anInvocation { if ([SUTRuntimeMethodHelper instancesRespondToSelector:anInvocation.selector]) { [anInvocation invokeWithTarget:_helper]; } }
轉發和多繼承
轉發和繼承相似,一個Object把消息轉發出去就好像它繼承了另一個Object的方法一樣。
Message消息的參考文章
- Message forwardinghttps://mikeash.com/pyblog/friday-qa-2009-03-27-objective-c-message-forwarding.html
- Objective-c messaginghttps://www.mikeash.com/pyblog/friday-qa-2009-03-20-objective-c-messaging.html
- The faster objc_msgSendhttp://www.mulle-kybernetik.com/artikel/Optimization/opti-9.html
Method Swizzling
是改變一個selector實際實現的技術,可以在運行時修改selector對應的函數來修改Method的實現。前面的消息轉發很強大,但是需要能夠修改對應類的源碼,但是對於有些類無法修改其源碼時又要更改其方法實現時可以使用Method Swizzling,通過重新映射方法來達到目的,但是跟消息轉發比起來調試會困難。
使用method swizzling需要注意的問題
- Swizzling應該總在+load中執行:Objective-C在運行時會自動調用類的兩個方法+load和+initialize。+load會在類初始加載時調用,和+initialize比較+load能保證在類的初始化過程中被加載
- Swizzling應該總是在dispatch_once中執行:swizzling會改變全局狀態,所以在運行時採取一些預防措施,使用dispatch_once就能夠確保代碼不管有多少線程都只被執行一次。這將成爲method swizzling的最佳實踐。
- Selector,Method和Implementation:這幾個之間關係可以這樣理解,一個類維護一個運行時可接收的消息分發表,分發表中每個入口是一個Method,其中key是一個特定的名稱,及SEL,與其對應的實現是IMP即指向底層C函數的指針。
舉例說明如何使用Method Swizzling對一個類中注入一些我們的新的操作。
#import <objc/runtime.h> @implementation UIViewController (Tracking) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; // When swizzling a class method, use the following: // Class class = object_getClass((id)self); //通過method swizzling修改了UIViewController的@selector(viewWillAppear:)的指針使其指向了自定義的xxx_viewWillAppear SEL originalSelector = @selector(viewWillAppear:); SEL swizzledSelector = @selector(xxx_viewWillAppear:); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); //如果類中不存在要替換的方法,就先用class_addMethod和class_replaceMethod函數添加和替換兩個方法實現。但如果已經有了要替換的方法,就調用method_exchangeImplementations函數交換兩個方法的Implementation。 if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } }); } #pragma mark - Method Swizzling - (void)xxx_viewWillAppear:(BOOL)animated { [self xxx_viewWillAppear:animated]; NSLog(@"viewWillAppear: %@", self); } @end
method_exchangeImplementations做的事情和如下代碼是一樣的
IMP imp1 = method_getImplementation(m1); IMP imp2 = method_getImplementation(m2); method_setImplementation(m1, imp2); method_setImplementation(m2, imp1);
另一種Method Swizzling的實現
- (void)replacementReceiveMessage:(const struct BInstantMessage *)arg1 { NSLog(@"arg1 is %@", arg1); [self replacementReceiveMessage:arg1]; } + (void)load { SEL originalSelector = @selector(ReceiveMessage:); SEL overrideSelector = @selector(replacementReceiveMessage:); Method originalMethod = class_getInstanceMethod(self, originalSelector); Method overrideMethod = class_getInstanceMethod(self, overrideSelector); if (class_addMethod(self, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) { class_replaceMethod(self, overrideSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, overrideMethod); } }
這裏有幾個關於Method Swizzling的資源可以參考
- How do I implement method swizzling?http://stackoverflow.com/questions/5371601/how-do-i-implement-method-swizzling
- Method Swizzlinghttp://nshipster.com/method-swizzling/
- What are the Dangers of Method Swizzling in Objective C?http://stackoverflow.com/questions/5339276/what-are-the-dangers-of-method-swizzling-in-objective-c
- JRSwizzlehttps://github.com/rentzsch/jrswizzle
Protocol和Category
基礎數據類型
Category
指向分類的結構體的指針
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; // 類方法列表,Meta Class方法列表的子集 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 分類所實現的協議列表 }
Category裏面的方法加載過程,objc源碼中找到objc-os.mm,函數_objc_init就是runtime的加載入口由libSystem調用,開始初始化,之後objc-runtime-new.mm裏的map_images會加載map到內存,_read_images開始初始化這個map,這時會load所有Class,Protocol和Category,NSObject的+load方法就是這個時候調用的。下面是加載代碼
// Discover categories. for (EACH_HEADER) { category_t **catlist = _getObjc2CategoryList(hi, &count); for (i = 0; i < count; i++) { category_t *cat = catlist[i]; Class cls = remapClass(cat->cls); if (!cls) { // Category's target class is missing (probably weak-linked). // Disavow any knowledge of this category. catlist[i] = nil; if (PrintConnecting) { _objc_inform("CLASS: IGNORING category ???(%s) %p with " "missing weak-linked target class", cat->name, cat); } continue; } // Process this category. // First, register the category with its target class. // Then, rebuild the class's method lists (etc) if // the class is realized. BOOL classExists = NO; if (cat->instanceMethods || cat->protocols || cat->instanceProperties) { addUnattachedCategoryForClass(cat, cls, hi); if (cls->isRealized()) { remethodizeClass(cls); classExists = YES; } if (PrintConnecting) { _objc_inform("CLASS: found category -%s(%s) %s", cls->nameForLogging(), cat->name, classExists ? "on existing class" : ""); } } if (cat->classMethods || cat->protocols /* || cat->classProperties */) { addUnattachedCategoryForClass(cat, cls->ISA(), hi); if (cls->ISA()->isRealized()) { remethodizeClass(cls->ISA()); } if (PrintConnecting) { _objc_inform("CLASS: found category +%s(%s)", cls->nameForLogging(), cat->name); } } } } //調用remethodizeClass方法,在其實現裏調用attachCategoryMethods static void attachCategoryMethods(Class cls, category_list *cats, bool flushCaches) { if (!cats) return; if (PrintReplacedMethods) printReplacements(cls, cats); bool isMeta = cls->isMetaClass(); method_list_t **mlists = (method_list_t **) _malloc_internal(cats->count * sizeof(*mlists)); // Count backwards through cats to get newest categories first int mcount = 0; int i = cats->count; BOOL fromBundle = NO; while (i--) { method_list_t *mlist = cat_method_list(cats->list[i].cat, isMeta); if (mlist) { mlists[mcount++] = mlist; fromBundle |= cats->list[i].fromBundle; } } attachMethodLists(cls, mlists, mcount, NO, fromBundle, flushCaches); _free_internal(mlists); }
示例,下面的代碼會編譯錯誤,Runtime Crash還是會正常輸出
@interface NSObject (Sark) + (void)foo; @end @implementation NSObject (Sark) - (void)foo { NSLog(@"IMP: -[NSObject(Sark) foo]"); } @end int main(int argc, const char * argv[]) { @autoreleasepool { [NSObject foo]; [[NSObject new] foo]; } return 0; } //結果,正常輸出結果如下 2014-11-06 13:11:46.694 Test[14872:1110786] IMP: -[NSObject(Sark) foo] 2014-11-06 13:11:46.695 Test[14872:1110786] IMP: -[NSObject(Sark) foo]
objc runtime加載後NSObject的Sark Category被加載,頭文件+(void)foo沒有IMP,只會出現一個warning。被加到Class的Method list裏的方法只有-(void)foo,Meta Class的方法列表裏沒有。
執行[NSObject foo]時,會在Meta Class的Method list裏找,找不着就繼續往super class裏找,NSObject Meta Clas的super class是NSObject本身,這時在NSObject的Method list裏就有foo這個方法了,能夠正常輸出。
執行[[NSObject new] foo]就簡單的多了,[NSObject new]生成一個實例,實例的Method list是有foo方法的,於是正常輸出。
Protocol
Protocol其實就是一個對象結構體
typedef struct objc_object Protocol;
操作函數
Category操作函數信息都包含在objc_class中,我們可以通過objc_class的操作函數來獲取分類的操作函數信息。
@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的方法列表中
Runtime提供了Protocol的一系列函數操作,函數包括
// 返回指定的協議 Protocol * objc_getProtocol ( const char *name ); // 獲取運行時所知道的所有協議的數組 Protocol ** objc_copyProtocolList ( unsigned int *outCount ); // 創建新的協議實例 Protocol * objc_allocateProtocol ( const char *name ); // 在運行時中註冊新創建的協議 void objc_registerProtocol ( Protocol *proto ); //創建一個新協議後必須使用這個進行註冊這個新協議,但是註冊後不能夠再修改和添加新方法。 // 爲協議添加方法 void protocol_addMethodDescription ( Protocol *proto, SEL name, const char *types, BOOL isRequiredMethod, BOOL isInstanceMethod ); // 添加一個已註冊的協議到協議中 void protocol_addProtocol ( Protocol *proto, Protocol *addition ); // 爲協議添加屬性 void protocol_addProperty ( Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty ); // 返回協議名 const char * protocol_getName ( Protocol *p ); // 測試兩個協議是否相等 BOOL protocol_isEqual ( Protocol *proto, Protocol *other ); // 獲取協議中指定條件的方法的方法描述數組 struct objc_method_description * protocol_copyMethodDescriptionList ( Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount ); // 獲取協議中指定方法的方法描述 struct objc_method_description protocol_getMethodDescription ( Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod ); // 獲取協議中的屬性列表 objc_property_t * protocol_copyPropertyList ( Protocol *proto, unsigned int *outCount ); // 獲取協議的指定屬性 objc_property_t protocol_getProperty ( Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty ); // 獲取協議採用的協議 Protocol ** protocol_copyProtocolList ( Protocol *proto, unsigned int *outCount ); // 查看協議是否採用了另一個協議 BOOL protocol_conformsToProtocol ( Protocol *proto, Protocol *other );
Block
runtime中一些支持block操作的函數
// 創建一個指針函數的指針,該函數調用時會調用特定的block IMP imp_implementationWithBlock ( id block ); // 返回與IMP(使用imp_implementationWithBlock創建的)相關的block id imp_getBlock ( IMP anImp ); // 解除block與IMP(使用imp_implementationWithBlock創建的)的關聯關係,並釋放block的拷貝 BOOL imp_removeBlock ( IMP anImp );
測試代碼
@interface MyRuntimeBlock : NSObject @end @implementation MyRuntimeBlock @end IMP imp = imp_implementationWithBlock(^(id obj, NSString *str) { NSLog(@"%@", str); }); class_addMethod(MyRuntimeBlock.class, @selector(testBlock:), imp, "v@:@"); MyRuntimeBlock *runtime = [[MyRuntimeBlock alloc] init]; [runtime performSelector:@selector(testBlock:) withObject:@"hello world!"]; //結果 2014-11-09 14:03:19.779 [1172:395446] hello world!
Runtime的應用
獲取系統提供的庫相關信息
主要函數
// 獲取所有加載的Objective-C框架和動態庫的名稱 const char ** objc_copyImageNames ( unsigned int *outCount ); // 獲取指定類所在動態庫 const char * class_getImageName ( Class cls ); // 獲取指定庫或框架中所有類的類名 const char ** objc_copyClassNamesForImage ( const char *image, unsigned int *outCount );
通過這些函數就能夠獲取某個類所有的庫,以及某個庫中包含哪些類
NSLog(@"獲取指定類所在動態庫"); NSLog(@"UIView's Framework: %s", class_getImageName(NSClassFromString(@"UIView"))); NSLog(@"獲取指定庫或框架中所有類的類名"); const char ** classes = objc_copyClassNamesForImage(class_getImageName(NSClassFromString(@"UIView")), &outCount); for (int i = 0; i < outCount; i++) { NSLog(@"class name: %s", classes[i]); } //結果 2014-11-08 12:57:32.689 [747:184013] 獲取指定類所在動態庫 2014-11-08 12:57:32.690 [747:184013] UIView's Framework: /System/Library/Frameworks/UIKit.framework/UIKit 2014-11-08 12:57:32.690 [747:184013] 獲取指定庫或框架中所有類的類名 2014-11-08 12:57:32.691 [747:184013] class name: UIKeyboardPredictiveSettings 2014-11-08 12:57:32.691 [747:184013] class name: _UIPickerViewTopFrame 2014-11-08 12:57:32.691 [747:184013] class name: _UIOnePartImageView 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerViewSelectionBar 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerWheelView 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerViewTestParameters ......
對App的用戶行爲進行追蹤
就是用戶點擊時把事件記錄下來。一般比較做法就是在viewDidAppear裏記錄事件,這樣會讓這樣記錄事件的代碼遍佈整個項目中。繼承或類別也會有問題。這時利用Method Swizzling把一個方法的實現和另一個方法的實現進行替換。