常见使用:
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];
}