常見使用:
runtime 常見的使用有:動態交換兩個方法的實現、實現分類也可以添加屬性、實現NSCoding的自動歸檔和解檔、實現字典轉模型的自動轉換、Hook
可以到我的github下載完整版
動態交換兩個方法的實現
NSLog(@"------Normal-----\n");
ShowExchange *normarlTest = [ShowExchange new];
[normarlTest firstMethod];
NSLog(@"------Normal-----\n");
//交換實例方法
NSLog(@"------exchange-----\n");
Method m1 = class_getInstanceMethod([ShowExchange class], @selector(firstMethod));
Method m2 = class_getInstanceMethod([ShowExchange class], @selector(secondMethod));
method_exchangeImplementations(m1, m2);
ShowExchange *test = [ShowExchange new];
[test firstMethod];
NSLog(@"------exchange InstanceMethod-----\n");
實現分類也可以添加屬性
-(void)setWxsTitle:(NSString *)wxsTitle {
objc_setAssociatedObject(self, WXSAddPropertyKeyTitle, wxsTitle, OBJC_ASSOCIATION_RETAIN);
}
-(NSString *)wxsTitle {
return objc_getAssociatedObject(self, WXSAddPropertyKeyTitle);
}
實現NSCoding的自動歸檔和解檔
unsigned int outCount = 0;
Ivar *ivars = class_copyIvarList(self.class, &outCount);
for (int i = 0; i< outCount; i++) {
Ivar ivar = ivars[i];
const char *ivarName = ivar_getName(ivar);
NSString *ivarNameStr = [NSString stringWithUTF8String:ivarName];
NSString *setterName = [ivarNameStr substringFromIndex:1];
//解碼
id obj = [aDecoder decodeObjectForKey:setterName]; //要注意key與編碼的key是一致的
SEL setterSel = [self creatSetterWithKey:setterName];
if (obj) {
((void (*)(id ,SEL ,id))objc_msgSend)(self,setterSel,obj);
}
}
free(ivars);
實現字典轉模型的自動轉換
unsigned int outCount = 0;
objc_property_t *properties = class_copyPropertyList(self.class, &outCount);
for (int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propertyName = property_getName(property);
NSString *key = [NSString stringWithUTF8String:propertyName];
id value = nil;
if (![dict[key] isKindOfClass:[NSNull class]]) {
value = dict[key];
}
unsigned int count = 0;
objc_property_attribute_t *atts = property_copyAttributeList(property, &count);
objc_property_attribute_t att = atts[0];
NSString *type = [NSString stringWithUTF8String:att.value];
type = [type stringByReplacingOccurrencesOfString:@"“" withString:@""];
type = [type stringByReplacingOccurrencesOfString:@"@" withString:@""];
NSLog(@"type%@",type);
//數據為數組時
if ([value isKindOfClass:[NSArray class]]) {
Class class = NSClassFromString(key);
NSMutableArray *temArr = [[NSMutableArray alloc] init];
for (NSDictionary *tempDic in value) {
if (class) {
id model = [[class alloc] initWithDic:tempDic];
[temArr addObject:model];
}
}
value = temArr;
}
//數據為字典時
if ([value isKindOfClass:[NSDictionary class]] && ![type hasPrefix:@"NS"] ) {
Class class = NSClassFromString(key);
if (class) {
value = [[class alloc] initWithDic:value];
}
}
// 賦值
SEL setterSel = [self creatSetterWithKey:key];
if (setterSel != nil) {
((void (*)(id,SEL,id))objc_msgSend)(self,setterSel,value);
}
}
Hook
- (void)viewDidLoad {
[super viewDidLoad];
Method m1 = class_getInstanceMethod([self class], @selector(viewWillAppear:));
Method m2 = class_getInstanceMethod([self class], @selector(wxs_viewWillAppear:));
BOOL isSuccess = class_addMethod([self class], @selector(viewWillAppear:), method_getImplementation(m2), method_getTypeEncoding(m2));
if (isSuccess) {
// 添加成功:說明源方法m1現在的實現為交換方法m2的實現,現在將源方法m1的實現替換到交換方法m2中
class_replaceMethod([self class], @selector(wxs_viewWillAppear:), method_getImplementation(m1), method_getTypeEncoding(m1));
}else {
//添加失敗:說明源方法已經有實現,直接將兩個方法的實現交換即
method_exchangeImplementations(m1, m2);
}
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(@"viewWillAppear");
}
- (void)wxs_viewWillAppear:(BOOL)animated {
NSLog(@"Hook : 攔截到viewwillApear的實現,在其基礎上添加了這行代碼");
[self wxs_viewWillAppear:YES];
}