公司項(xiàng)目中用了一些 runtime 相關(guān)的知識, 初看時(shí)有些蒙, 雖然用的并不多, 但還是想著系統(tǒng)的把 runtime 相關(guān)的常用方法整理一下, 自己以后用著方便, 也希望對看到的朋友有所幫助.
一、runtime 簡介
runtime 簡稱運(yùn)行時(shí)眠蚂,是系統(tǒng)在運(yùn)行的時(shí)候的一些機(jī)制驹止,其中最主要的是消息機(jī)制萍诱。它是一套比較底層的純 C 語言 API, 屬于一個(gè) C 語言庫,包含了很多底層的 C 語言 API。我們平時(shí)編寫的 OC 代碼,在程序運(yùn)行過程時(shí)柿究,其實(shí)最終都是轉(zhuǎn)成了 runtime 的 C 語言代碼。如下所示:
// OC代碼:
[Person coding];
//運(yùn)行時(shí) runtime 會將它轉(zhuǎn)化成 C 語言的代碼:
objc_msgSend(Person, @selector(coding));
二黄选、相關(guān)函數(shù)
// 遍歷某個(gè)類所有的成員變量
class_copyIvarList
// 遍歷某個(gè)類所有的方法
class_copyMethodList
// 獲取指定名稱的成員變量
class_getInstanceVariable
// 獲取成員變量名
ivar_getName
// 獲取成員變量類型編碼
ivar_getTypeEncoding
// 獲取某個(gè)對象成員變量的值
object_getIvar
// 設(shè)置某個(gè)對象成員變量的值
object_setIvar
// 給對象發(fā)送消息
objc_msgSend
三蝇摸、相關(guān)應(yīng)用
- 更改屬性值
- 動(dòng)態(tài)添加屬性
- 動(dòng)態(tài)添加方法
- 交換方法的實(shí)現(xiàn)
- 攔截并替換方法
- 在方法上增加額外功能
- 歸檔解檔
- 字典轉(zhuǎn)模型
以上八種用法用代碼都實(shí)現(xiàn)了, 文末會貼出代碼地址.
四、代碼實(shí)現(xiàn)
要使用runtime,要先引入頭文件#import <objc/runtime.h>
4.1 更改屬性值
用 runtime 修改一個(gè)對象的屬性值
unsigned int count = 0;
// 動(dòng)態(tài)獲取類中的所有屬性(包括私有)
Ivar *ivar = class_copyIvarList(_person.class, &count);
// 遍歷屬性找到對應(yīng)字段
for (int i = 0; i < count; i ++) {
Ivar tempIvar = ivar[i];
const char *varChar = ivar_getName(tempIvar);
NSString *varString = [NSString stringWithUTF8String:varChar];
if ([varString isEqualToString:@"_name"]) {
// 修改對應(yīng)的字段值
object_setIvar(_person, tempIvar, @"更改屬性值成功");
break;
}
}
4.2 動(dòng)態(tài)添加屬性
用 runtime 為一個(gè)類添加屬性, iOS 分類里一般會這樣用, 我們建立一個(gè)分類, NSObject+NNAddAttribute.h
, 并添加以下代碼:
- (void)setName:(NSString *)name {
objc_setAssociatedObject(self, @"name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)name {
return objc_getAssociatedObject(self, @"name");
}
這樣只要引用 NSObject+NNAddAttribute.h
, 用 NSObject 創(chuàng)建的對象就會有一個(gè) name 屬性, 我們可以直接這樣寫:
NSObject *person = [NSObject new];
person.name = @"以夢為馬";
4.3 動(dòng)態(tài)添加方法
person 類中沒有 coding
方法,我們用 runtime 給 person 類添加了一個(gè)名字叫 coding
的方法搭伤,最終再調(diào)用coding方法做出相應(yīng). 下面代碼的幾個(gè)參數(shù)需要注意一下:
- (void)buttonClick:(UIButton *)sender {
/*
動(dòng)態(tài)添加 coding 方法
(IMP)codingOC 意思是 codingOC 的地址指針;
"v@:" 意思是,v 代表無返回值 void啡专,如果是 i 則代表 int;@代表 id sel; : 代表 SEL _cmd;
“v@:@@” 意思是制圈,兩個(gè)參數(shù)的沒有返回值们童。
*/
class_addMethod([_person class], @selector(coding), (IMP)codingOC, "v@:");
// 調(diào)用 coding 方法響應(yīng)事件
if ([_person respondsToSelector:@selector(coding)]) {
[_person performSelector:@selector(coding)];
self.testLabelText = @"添加方法成功";
} else {
self.testLabelText = @"添加方法失敗";
}
}
// 編寫 codingOC 的實(shí)現(xiàn)
void codingOC(id self,SEL _cmd) {
NSLog(@"添加方法成功");
}
4.4 交換方法的實(shí)現(xiàn)
某個(gè)類有兩個(gè)方法, 比如 person 類有兩個(gè)方法, coding 方法與 eating 方法, 我們用 runtime 交換一下這兩個(gè)方法, 就會出現(xiàn)這樣的情況, 當(dāng)我們調(diào)用 coding 的時(shí)候, 執(zhí)行的是 eating, 當(dāng)我們調(diào)用 eating 的時(shí)候, 執(zhí)行的是 coding, 如下面的動(dòng)態(tài)效果圖.
Method oriMethod = class_getInstanceMethod(_person.class, @selector(coding));
Method curMethod = class_getInstanceMethod(_person.class, @selector(eating));
method_exchangeImplementations(oriMethod, curMethod);
4.5 攔截并替換方法
這個(gè)功能和上面的其實(shí)有些類似, 攔截并替換方法可以攔截并替換同一個(gè)類的, 也可以在兩個(gè)類之間進(jìn)行, 我這里用了兩個(gè)不同的類, 下面是簡單的代碼實(shí)現(xiàn).
_person = [NNPerson new];
_library = [NNLibrary new];
self.testLabelText = [_library libraryMethod];
Method oriMethod = class_getInstanceMethod(_person.class, @selector(changeMethod));
Method curMethod = class_getInstanceMethod(_library.class, @selector(libraryMethod));
method_exchangeImplementations(oriMethod, curMethod);
4.6 在方法上增加額外功能
這個(gè)使用場景還是挺多的, 比如我們需要記錄 APP 中某一個(gè)按鈕的點(diǎn)擊次數(shù), 這個(gè)時(shí)候我們便可以利用 runtime 來實(shí)現(xiàn)這個(gè)功能. 我這里寫了個(gè) UIButton 的子類, 然后在 + (void)load
中用 runtime 給它增加了一個(gè)功能, 核心代碼及實(shí)現(xiàn)效果圖如下:
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method oriMethod = class_getInstanceMethod(self.class, @selector(sendAction:to:forEvent:));
Method cusMethod = class_getInstanceMethod(self.class, @selector(customSendAction:to:forEvent:));
// 判斷自定義的方法是否實(shí)現(xiàn), 避免崩潰
BOOL addSuccess = class_addMethod(self.class, @selector(sendAction:to:forEvent:), method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
if (addSuccess) {
// 沒有實(shí)現(xiàn), 將源方法的實(shí)現(xiàn)替換到交換方法的實(shí)現(xiàn)
class_replaceMethod(self.class, @selector(customSendAction:to:forEvent:), method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
} else {
// 已經(jīng)實(shí)現(xiàn), 直接交換方法
method_exchangeImplementations(oriMethod, cusMethod);
}
});
}
4.7 歸檔解檔
當(dāng)我們使用 NSCoding 進(jìn)行歸檔及解檔時(shí), 如果不用 runtime, 那么不管模型里面有多少屬性, 我們都需要對其實(shí)現(xiàn)一遍 encodeObject
和 decodeObjectForKey
方法, 如果模型里面有 10000 個(gè)屬性, 那么我們就需要寫 10000 句encodeObject
和 decodeObjectForKey
方法, 這個(gè)時(shí)候用 runtime, 便可以充分體驗(yàn)其好處(以下只是核心代碼, 具體代碼請見 demo).
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = 0;
// 獲取類中所有屬性
Ivar *ivars = class_copyIvarList(self.class, &count);
// 遍歷屬性
for (int i = 0; i < count; i ++) {
// 取出 i 位置對應(yīng)的屬性
Ivar ivar = ivars[i];
// 查看屬性
const char *name = ivar_getName(ivar);
NSString *key = [NSString stringWithUTF8String:name];
// 利用 KVC 進(jìn)行取值,根據(jù)屬性名稱獲取對應(yīng)的值
id value = [self valueForKey:key];
[aCoder encodeObject:value forKey:key];
}
free(ivars);
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int count = 0;
// 獲取類中所有屬性
Ivar *ivars = class_copyIvarList(self.class, &count);
// 遍歷屬性
for (int i = 0; i < count; i ++) {
// 取出 i 位置對應(yīng)的屬性
Ivar ivar = ivars[i];
// 查看屬性
const char *name = ivar_getName(ivar);
NSString *key = [NSString stringWithUTF8String:name];
// 進(jìn)行解檔取值
id value = [aDecoder decodeObjectForKey:key];
// 利用 KVC 對屬性賦值
[self setValue:value forKey:key];
}
}
return self;
}
4.8 字典轉(zhuǎn)模型
字典轉(zhuǎn)模型我們通常用的都是第三方, MJExtension, YYModel 等, 但也有必要了解一下其實(shí)現(xiàn)方式: 遍歷模型中的所有屬性鲸鹦,根據(jù)模型的屬性名慧库,去字典中查找對應(yīng)的 key,取出對應(yīng)的值馋嗜,給模型的屬性賦值完沪。
/** 字典轉(zhuǎn)模型 **/
+ (instancetype)modelWithDict:(NSDictionary *)dict {
id objc = [[self alloc] init];
unsigned int count = 0;
// 獲取成員屬性數(shù)組
Ivar *ivarList = class_copyIvarList(self, &count);
// 遍歷所有的成員屬性名
for (int i = 0; i < count; i ++) {
// 獲取成員屬性
Ivar ivar = ivarList[i];
// 獲取成員屬性名
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
NSString *key = [ivarName substringFromIndex:1];
// 從字典中取出對應(yīng) value 給模型屬性賦值
id value = dict[key];
// 獲取成員屬性類型
NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// 判斷 value 是不是字典
if ([value isKindOfClass:[NSDictionary class]]) {
ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
ivarType = [ivarType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
Class modalClass = NSClassFromString(ivarType);
// 字典轉(zhuǎn)模型
if (modalClass) {
// 字典轉(zhuǎn)模型
value = [modalClass modelWithDict:value];
}
}
if ([value isKindOfClass:[NSArray class]]) {
// 判斷對應(yīng)類有沒有實(shí)現(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 modelWithDict:dict];
[arrM addObject:model];
}
// 把模型數(shù)組賦值給value
value = arrM;
}
}
// KVC 字典轉(zhuǎn)模型
if (value) {
[objc setValue:value forKey:key];
}
}
return objc;
}
上面的所有代碼都可以在這里下載: runtime 練習(xí): NNRuntimeTest