一、runtime 運行時機(jī)制
- Objective-C語言是一門動態(tài)語言,它將很多靜態(tài)語言在編譯和鏈接時期做的事放到了運行時來處理。這種動態(tài)語言的優(yōu)勢在于:我們寫代碼時更具靈活性,如我們可以把消息轉(zhuǎn)發(fā)給我們想要的對象,或者隨意交換一個方法的實現(xiàn)等。
- 這種特性意味著Objective-C不僅需要一個編譯器,還需要一個運行時系統(tǒng)來執(zhí)行編譯的代碼。對于Objective-C來說,這個運行時系統(tǒng)就像一個操作系統(tǒng)一樣:它讓所有的工作可以正常的運行。這個運行時系統(tǒng)即Objc Runtime。Objc Runtime其實是一個Runtime庫,它基本上是用C和匯編寫的,這個庫使得C語言有了面向?qū)ο蟮哪芰Α?/li>
- 對于C語言,函數(shù)的調(diào)用在編譯的時候會決定調(diào)用哪個函數(shù)。對于OC的函數(shù),屬于動態(tài)調(diào)用過程,在編譯的時候并不能決定真正調(diào)用哪個函數(shù),只有在真正運行的時候才會根據(jù)函數(shù)的名稱找到對應(yīng)的函數(shù)來調(diào)用。
- 在編譯階段,OC可以調(diào)用任何函數(shù),即使這個函數(shù)并未實現(xiàn),只要聲明過就不會報錯。
在編譯階段,C語言調(diào)用未實現(xiàn)的函數(shù)就會報錯。
二、運行時的作用
- 能獲得某個類的所有成員變量
- 能獲得某個類的所有屬性
- 能獲得某個類的所有方法
- 交換方法實現(xiàn)
- 能動態(tài)添加一個成員變量
- 能動態(tài)添加一個屬性
- 字典轉(zhuǎn)模型
- runtime歸檔/反歸檔
常見的函數(shù),頭文件
#import<objc/runtime.h> : //成員變量,類,方法
class_copyIvarList : 獲得某個類內(nèi)部的所有成員變量
class_copyMethodList : 獲得某個類內(nèi)部的所有方法
class_getInstanceMethod : 獲得某個具體的實例方法 (對象方法,減號-開頭)
class_getClassMethod : 獲得某個具體的類方法 (加號+開頭)
method_exchangeImplementations : 交換兩個方法的實現(xiàn)
#import<objc/message.h> : //消息機(jī)制
objc_msgSend(...)
三、應(yīng)用場景
場景1 ----------------------- runtime 發(fā)送消息 ----------------------
方法的調(diào)用本質(zhì)是,對象發(fā)送消息
objc/msgSend 只有對象才能發(fā)送消息,因此以objc開頭
導(dǎo)入 #import <objc/message.h> 或者直接導(dǎo)入 #import <objc/runtime.h>
注意 Xcode 6 之后代碼檢查 單獨使用<objc/message.h>會報錯
builtSeting 修改 Enable Strict Checking of objc_msgSend Calls -> NO 才能調(diào)用 objc_msgSend
我們創(chuàng)建一個對象Dog 自定義一個實例方法和類方法,并實現(xiàn)方法
#import <Foundation/Foundation.h>
@interface Dog : NSObject
- (void)run;
+ (void)eat;
- (void)run
{
NSLog(@"一只狗正在奔跑。。。。");
}
+ (void)eat
{
NSLog(@"一只狗正在吃。。。。");
}
@end
然后我們在vc里面使用
// 創(chuàng)建對象 -> 調(diào)用方法
Dog *d = [[Dog alloc] init];
// 調(diào)用方法 -> 實例方法
// [d run];
// 系統(tǒng)底層本質(zhì) -> 讓對象發(fā)消息
objc_msgSend(d, @selector(run)); // 等同于 [d run];
// 調(diào)用方法 -> 類方法
// [Dog eat];
objc_msgSend([Dog class], @selector(eat)); // 等同于 [Dog eat];
消息機(jī)制原理
對象根據(jù)方法編號SEL去映射表查找對應(yīng)方法的實現(xiàn),即我們在調(diào)用實例方法的時候,其實是實例對象d,在發(fā)送消息,消息的實現(xiàn),其實是SEL,根據(jù)方法編號,去映射表查找對應(yīng)方法的實現(xiàn).類方法本質(zhì)是[Dog class],發(fā)什么消息.
場景2 ------------------- runtime 交換方法 ----------------------
使用場景,系統(tǒng)自帶方法功能不夠用,給系統(tǒng)自帶的方法擴(kuò)展一些功能,并保存原有功能.
- 實現(xiàn)方法 1 -> 繼承系統(tǒng)的類, 重寫方法.
- 實現(xiàn)方法 2 -> runtime 交換方法
案例:這里我們寫一個UIImage的類目,來保證UIImage,不會被渲染,同時,如果圖片為空,會打印提示.
#import <UIKit/UIKit.h>
@interface UIImage (Image)
// 創(chuàng)建一個類方法
// 傳入 一個字符串 -> 返回 不被 渲染的原始圖片
+ (id)ImageOriginalWithStrName:(NSString *)name;
@end
在.m進(jìn)行實現(xiàn), 使用method_exchangeImplementations(method1, method2)方法交換,詳情看代碼注釋.
#import "UIImage+Image.h"
#import <objc/runtime.h>
@implementation UIImage (Image)
// 加載內(nèi)存時調(diào)用
+ (void)load
{
// 交換方法
// 獲取 ImageOriginalWithStrName: 方法
Method imageWithName = class_getClassMethod(self, @selector(ImageOriginalWithStrName:));
// 獲取 imageName 方法
Method imageName = class_getClassMethod(self, @selector(imageNamed:));
// 交換方法地址, 相當(dāng)于交換實現(xiàn)
method_exchangeImplementations(imageWithName, imageName);
}
// 注意 // 這里 返回值是一個函數(shù)結(jié)果類型 使用instancetype 會產(chǎn)生類型不匹配, 所以使用id
// 不能在改分類UIImage中重寫 imageNamed:因為系統(tǒng)會把imageNamed:原來的功能覆蓋掉
// 分類中不能調(diào)用super本身
+ (id)ImageOriginalWithStrName:(NSString *)name
{
UIImage *image = [[self ImageOriginalWithStrName:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
if (image == nil) {
NSLog(@"加載圖片為空...");
}
return image;
}
@end
在vc中使用
UIImage *image = [UIImage imageNamed:@"123"];
// 這里通過runtime 交換自定義方法 和 UIImage 自身的imageNamed: 來實現(xiàn)圖片加載 不被渲染
// 代碼在 #import "UIImage+Image.h" 類目中
// 傳入了一個空123
場景3-------------------- runtime 動態(tài)添加方法 ---------------------
開發(fā)場景:
如果一個類方法非常多,加載類到內(nèi)存中的時候,會比較耗費資源,需要給給個方法生成映射表,這里可以使用動態(tài)添加方法給某個類.
經(jīng)典面試題 有沒有使用過 performSelector 其實主要是想問你有沒有動態(tài)添加過方法.
// 下面是簡單使用,繼續(xù)以Dog 對象為例
[d performSelector:@selector(jump)];
// 默認(rèn)的狗 沒有jump 這個方法實現(xiàn), 直接調(diào)用會出錯,可以使用 performSelector 調(diào)用就不會出錯.
// --> 動態(tài)添加方法 不會報錯
然后我們可以到Dog對象中進(jìn)行添加動態(tài)方法
// void(*)()
// 默認(rèn)方法 都有兩個隱式參數(shù)
// 定義添加的方法
void jump (id self, SEL sel)
{
NSLog(@" eat ...... %@ --- %@ ", self, NSStringFromSelector(sel));
}
// 當(dāng)一個對象調(diào)用未實現(xiàn)的方法,會調(diào)用(+ (BOOL)resolveInstanceMethod:(SEL)sel
)這個方法處理,并且會把這個對應(yīng)方法列表傳過來
// 所以動態(tài)添加方法, 我們可以在這里做判斷, 為我們未實現(xiàn)的方法動態(tài)添加自己的方法.
+ (BOOL)resolveInstanceMethod:(SEL)sel
{
if (sel == @selector(jump)) {
// <#__unsafe_unretained Class cls#> 參數(shù)1 給哪個類添加方法
// <#SEL name#> 參數(shù)2 添加放啊編號
// <#IMP imp#> 參數(shù)3 添加方法函數(shù)實現(xiàn) (函數(shù)地址)
// <#const char *types#> 參數(shù)4 函數(shù)的類型 (返回值 + 參數(shù)類型) v:void @:對象-> self :表示SEL -> _cmd
class_addMethod(self, @selector(jump), jump, "v@:");
}
return [super resolveInstanceMethod:sel];
}
// 實現(xiàn)動態(tài)添加方法后,在vc中,使用不會出錯.
場景4 -------------------- runtime 給分類添加屬性 ---------------
原理給一個類聲明屬性,其實是本質(zhì)就是給這個類添加關(guān)聯(lián),并不是直接把這個值的內(nèi)存空間添加到內(nèi)存空間
案例 : 這里在類目中對NSObject擴(kuò)展name 屬性 可以直接給屬性賦值使用
#import <Foundation/Foundation.h>
@interface NSObject (Property)
@property (nonatomic, strong) NSString *name; // 添加一個name屬性
@end
在.m 中使用objc_setAssociatedObject動態(tài)添加方法
import "NSObject+Property.h"
#import <objc/runtime.h>
static const char *key = "name";
@implementation NSObject (Property)
- (NSString *)name
{
// 根據(jù)關(guān)聯(lián)的key,獲取關(guān)聯(lián)的值
return objc_getAssociatedObject(self, key);
}
- (void)setName:(NSString *)name
{
// 參數(shù)1 <#id object#> 給那個對象添加關(guān)聯(lián)
// 參數(shù)2 <#const void *key#> 關(guān)聯(lián)的key 值,通過這個key 值獲取
// 參數(shù)3 <#id value#> 關(guān)聯(lián)的value
// 參數(shù)4 <#objc_AssociationPolicy policy#> 關(guān)聯(lián)的策略
// typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
// OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
// OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
// * The association is not made atomically. */
// OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
// * The association is not made atomically. */
// OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
// * The association is made atomically. */
// OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
// * The association is made atomically. */
// };
objc_setAssociatedObject(self, key, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end
VC 使用.能正常獲取該屬性,并能操作使用
NSObject *objc = [[NSObject alloc] init];
objc.name = @"xxx";
NSLog(@"%@", objc.name);
```
> 場景5 -------------------- runtime 字典轉(zhuǎn)模型 ---------------
設(shè)計模型 : 字典轉(zhuǎn)模型的第一步
模型屬性, 通常需要和字典中的key一一對應(yīng)
通過創(chuàng)建一個分類,專門根據(jù)字典生成對應(yīng)屬性的字符串 (高效率的字典轉(zhuǎn)模型)
我們可以正常通過寫類目來實現(xiàn) NSObject+DictionaryToModel
// 自動打印屬性字符串
// 寫一個類方法
- (void)transformToModelByDictionary:(NSDictionary *)dict;
// 實現(xiàn)
-
(void)transformToModelByDictionary:(NSDictionary *)dict
{// 根據(jù)類別拼接屬性字符串代碼
NSMutableString *str = [NSMutableString string];// 遍歷字典,把字典中的所有key取出來;生成對應(yīng)的屬性代碼
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// 對各類新進(jìn)行分類, 抽取出來 NSString *type; // 需要 理解 系統(tǒng)底層 數(shù)據(jù)結(jié)構(gòu)類型 // 可以自行斷點查看 各類型底層類型 if ([obj isKindOfClass:NSClassFromString(@"__NSCFString")]) { type = @"NSString"; } else if ([obj isKindOfClass:NSClassFromString(@"__NSArrayI")]) { type = @"NSArray"; } else if ([obj isKindOfClass:NSClassFromString(@"__NSArrayM")]) { type = @"NSMutableArray"; } else if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]) { type = @"NSNumber"; } else if ([obj isKindOfClass:NSClassFromString(@"__NSDictionaryI")]) { type = @"NSDictionary"; } else if ([obj isKindOfClass:NSClassFromString(@"__NSDictionaryM")]) { type = @"NSMutableDictionary"; } // 屬性字符串 NSString *property; if ([type containsString:@"NS"]) { property = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@", type, key]; } else { property = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@", type, key]; } // 每生成一對屬性字符串 就自動換行 [str appendFormat:@"\n%@\n", property];
}];
// 打印出拼接的字符串
NSLog(@"對應(yīng)屬性 -> %@", str);
}
vc中測試
NSArray *array = [NSArray arrayWithObjects:@1, @2, @3, @4, nil];
NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"dfdf" forKey:@"dfsdf"];
[NSObject transformToModelByDictionary:@{@"name" : @"str", @"num" : array, @"count" : @0, @"hah": arr, @"dic" : dic}];

// 字典轉(zhuǎn)模型KVC方式
dic setValuesForKeysWithDictionary:<#(nonnull NSDictionary<NSString *,id> *)#>
必須保證屬性和字典中key值一一對應(yīng), 如果不一致 會調(diào)用 setValue:forUndefinedKey: 重寫該方法可以覆蓋系統(tǒng)方法 可以繼續(xù)KVC 字典轉(zhuǎn)模型
字典轉(zhuǎn)模型 -> runtime
-> MJEXtension
import "NSObject+Model.h"
import <objc/runtime.h>
@implementation NSObject (Model)
- (instancetype)modelWithDictionary:(NSDictionary *)dictionary
{
// 思路:遍歷模型中所有屬性-》使用運行時
// 0.創(chuàng)建對應(yīng)的對象
id objc = [[self alloc] init];
// 1.利用runtime給對象中的成員屬性賦值
// class_copyIvarList:獲取類中的所有成員屬性
// Ivar:成員屬性的意思
// 第一個參數(shù):表示獲取哪個類中的成員屬性
// 第二個參數(shù):表示這個類有多少成員屬性,傳入一個Int變量地址,會自動給這個變量賦值
// 返回值Ivar *:指的是一個ivar數(shù)組,會把所有成員屬性放在一個數(shù)組中,通過返回的數(shù)組就能全部獲取到。
/* 類似下面這種寫法
Ivar ivar;
Ivar ivar1;
Ivar ivar2;
// 定義一個ivar的數(shù)組a
Ivar a[] = {ivar,ivar1,ivar2};
// 用一個Ivar *指針指向數(shù)組第一個元素
Ivar *ivarList = a;
// 根據(jù)指針訪問數(shù)組第一個元素
ivarList[0];
*/
unsigned int count;
// 獲取類中的所有成員屬性
Ivar *ivarList = class_copyIvarList(self, &count);
for (int i = 0; i < count; i++) {
// 根據(jù)角標(biāo),從數(shù)組取出對應(yīng)的成員屬性
Ivar ivar = ivarList[i];
// 獲取成員屬性名
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
// 處理成員屬性名->字典中的key
// 從第一個角標(biāo)開始截取
NSString *key = [name substringFromIndex:1];
// 根據(jù)成員屬性名去字典中查找對應(yīng)的value
id value = dictionary[key];
// 二級轉(zhuǎn)換:如果字典中還有字典,也需要把對應(yīng)的字典轉(zhuǎn)換成模型
// 判斷下value是否是字典
if ([value isKindOfClass:[NSDictionary class]]) {
// 字典轉(zhuǎn)模型
// 獲取模型的類對象,調(diào)用modelWithDict
// 模型的類名已知,就是成員屬性的類型
// 獲取成員屬性類型
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// 生成的是這種@"@\"User\"" 類型 -》 @"User" 在OC字符串中 \" -> ",\是轉(zhuǎn)義的意思,不占用字符
// 裁剪類型字符串
NSRange range = [type rangeOfString:@"\""];
type = [type substringFromIndex:range.location + range.length];
range = [type rangeOfString:@"\""];
// 裁剪到哪個角標(biāo),不包括當(dāng)前角標(biāo)
type = [type substringToIndex:range.location];
// 根據(jù)字符串類名生成類對象
Class modelClass = NSClassFromString(type);
if (modelClass) { // 有對應(yīng)的模型才需要轉(zhuǎn)
// 把字典轉(zhuǎn)模型
value = [modelClass modelWithDictionary:value];
}
}
// 三級轉(zhuǎn)換:NSArray中也是字典,把數(shù)組中的字典轉(zhuǎn)換成模型.
// 判斷值是否是數(shù)組
if ([value isKindOfClass:[NSArray class]]) {
// 判斷對應(yīng)類有沒有實現(xiàn)字典數(shù)組轉(zhuǎn)模型數(shù)組的協(xié)議
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 轉(zhuǎn)換成id類型,就能調(diào)用任何對象的方法
id idSelf = self;
// 獲取數(shù)組中字典對應(yīng)的模型
NSString *type = [idSelf arrayContainModelClass][key];
// 生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrM = [NSMutableArray array];
// 遍歷字典數(shù)組,生成模型數(shù)組
for (NSDictionary *dict in value) {
// 字典轉(zhuǎn)模型
id model = [classModel modelWithDictionary:dict];
[arrM addObject:model];
}
// 把模型數(shù)組賦值給value
value = arrM;
}
}
if (value) { // 有值,才需要給模型的屬性賦值
// 利用KVC給模型中的屬性賦值
[objc setValue:value forKey:key];
}
}
return objc;
}
@end
// 測試
NSMutableDictionary *di = [NSMutableDictionary dictionary];
NSMutableArray *a = [NSMutableArray array];
[a addObject:dic];
[di setValue:a forKey:@"dic"];
id model = [NSObject modelWithDictionary:dic];
NSLog(@"%@", model);
id dmodel = [NSObject modelWithDictionary:di];
NSLog(@"%@", dmodel);
> 場景6 -------------------- runtime 快速歸檔 ---------------
主要還是通過class_copyIvarList,遍歷對象屬性,來做事情.
import <Foundation/Foundation.h>
@interface NSObject (Extension)
- (NSArray *)ignoredNames;
- (void)encode:(NSCoder *)aCoder;
- (void)decode:(NSCoder *)aDecoder;
@end
import "NSObject+Extension.h"
import <objc/runtime.h>
@implementation NSObject (Extension)
-
(void)decode:(NSCoder *)aDecoder {
// 一層層父類往上查找,對父類的屬性執(zhí)行歸解檔方法
Class c = self.class;
while (c &&c != [NSObject class]) {unsigned int outCount = 0; Ivar *ivars = class_copyIvarList(c, &outCount); for (int i = 0; i < outCount; i++) { Ivar ivar = ivars[i]; NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 如果有實現(xiàn)該方法再去調(diào)用 if ([self respondsToSelector:@selector(ignoredNames)]) { if ([[self ignoredNames] containsObject:key]) continue; } id value = [aDecoder decodeObjectForKey:key]; [self setValue:value forKey:key]; } free(ivars); c = [c superclass];
}
}
-
(void)encode:(NSCoder *)aCoder {
// 一層層父類往上查找,對父類的屬性執(zhí)行歸解檔方法
Class c = self.class;
while (c &&c != [NSObject class]) {unsigned int outCount = 0; Ivar *ivars = class_copyIvarList([self class], &outCount); for (int i = 0; i < outCount; i++) { Ivar ivar = ivars[i]; NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 如果有實現(xiàn)該方法再去調(diào)用 if ([self respondsToSelector:@selector(ignoredNames)]) { if ([[self ignoredNames] containsObject:key]) continue; } id value = [self valueForKeyPath:key]; [aCoder encodeObject:value forKey:key]; } free(ivars); c = [c superclass];
}
}
// 設(shè)置需要忽略的屬性
- (NSArray *)ignoredNames {
return @[@"bone"];
}
// 在需要歸解檔的對象中實現(xiàn)下面方法即可:
//// 在系統(tǒng)方法內(nèi)來調(diào)用我們的方法
//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
// if (self = [super init]) {
// [self decode:aDecoder];
// }
// return self;
//}
//
//- (void)encodeWithCoder:(NSCoder *)aCoder {
// [self encode:aCoder];
//}
@end
> ----- 最后使用runtime 來寫了通過 block回調(diào) 直接調(diào)用手勢識別的action ---------------
import <UIKit/UIKit.h>
typedef void(^XXWGestureBlock)(id gestureRecognizer);
@interface UIGestureRecognizer (Block)
/**
- 使用類方法 初始化 添加手勢
- @param block 手勢回調(diào)
- @return block 內(nèi)部 action
- 使用 __unsafe_unretained __typeof(self) weakSelf = self;
- 防止循環(huán)引用
*/
- (instancetype)xxw_gestureRecognizerWithActionBlock:(XXWGestureBlock)block;
@end
import "UIGestureRecognizer+Block.h"
import <objc/runtime.h>
static const int target_key;
@implementation UIGestureRecognizer (Block)
- (instancetype)xxw_gestureRecognizerWithActionBlock:(XXWGestureBlock)block {
return [[self alloc]initWithActionBlock:block];
}
- (instancetype)initWithActionBlock:(XXWGestureBlock)block {
self = [self init];
[self addActionBlock:block];
[self addTarget:self action:@selector(invoke:)];
return self;
}
/**
- Returns the value associated with a given object for a given key.
- @param object The source object for the association.
- @param key The key for the association.
- @return The value associated with the key \e key for \e object.
- @see objc_setAssociatedObject
*/
//OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
//__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
(void)addActionBlock:(XXWGestureBlock)block {
if (block) {
objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
}(void)invoke:(id)sender {
XXWGestureBlock block = objc_getAssociatedObject(self, &target_key);
if (block) {
block(sender);
}
}
@end
VC 中使用,我們可以直接調(diào)用手勢對象,來實現(xiàn)action,是不是很方便.
[self.view addGestureRecognizer:[UITapGestureRecognizer xxw_gestureRecognizerWithActionBlock:^(id gestureRecognizer) {
NSLog(@"點擊-------");
}]];
[self.view addGestureRecognizer:[UILongPressGestureRecognizer xxw_gestureRecognizerWithActionBlock:^(id gestureRecognizer) {
NSLog(@"長按-------");
}]];
> 總結(jié):通過學(xué)習(xí)使用 runtime,我們能更好來了解體會oc底層的運行機(jī)制,同時,我們可以使用runtime,來獲取系統(tǒng)底層私有方法和屬性來使用,動態(tài)的添加屬性和方法,在有些場景下使用非常的高效,同時我們可以使用runtime,開完成很多好的Category,來高效開發(fā).本文章只是對runtime的一些基礎(chǔ)知識的歸納,能夠讓初學(xué)者,更好更快的理解runtime,力圖起個拋磚引玉的作用。還有許多關(guān)于runtime有意思東西還需要讀者自己去探索發(fā)現(xiàn)。
runtime 深入學(xué)習(xí) 推薦 ->
顧鵬:[http://tech.glowing.com/cn/objective-c-runtime/](http://tech.glowing.com/cn/objective-c-runtime/)
楊瀟玉:[http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/](http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/)
南峰子:[http://southpeak.github.io/blog/2014/10/25/objective-c-runtime-yun-xing-shi-zhi-lei-yu-dui-xiang/](http://southpeak.github.io/blog/2014/10/25/objective-c-runtime-yun-xing-shi-zhi-lei-yu-dui-xiang/)
葉純俊[http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective[nil]c-runtime(1)[nil]-self-and-super/](http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime(1)%5Bnil%5D-self-and-super/)
本文中 [所有代碼傳送門](https://github.com/CivelXu/runtime-.git)