常見(jiàn)使用:
runtime 常見(jiàn)的使用有:動(dòng)態(tài)交換兩個(gè)方法的實(shí)現(xiàn)市怎、實(shí)現(xiàn)分類也可以添加屬性辛慰、實(shí)現(xiàn)NSCoding的自動(dòng)歸檔和解檔、實(shí)現(xiàn)字典轉(zhuǎn)模型的自動(dòng)轉(zhuǎn)換驰弄、Hook
可以到我的github下載完整版
動(dòng)態(tài)交換兩個(gè)方法的實(shí)現(xiàn)
NSLog(@"------Normal-----\n");
ShowExchange *normarlTest = [ShowExchange new];
[normarlTest firstMethod];
NSLog(@"------Normal-----\n");
//交換實(shí)例方法
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");
實(shí)現(xiàn)分類也可以添加屬性
-(void)setWxsTitle:(NSString *)wxsTitle {
objc_setAssociatedObject(self, WXSAddPropertyKeyTitle, wxsTitle, OBJC_ASSOCIATION_RETAIN);
}
-(NSString *)wxsTitle {
return objc_getAssociatedObject(self, WXSAddPropertyKeyTitle);
}
實(shí)現(xiàn)NSCoding的自動(dòng)歸檔和解檔
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);
實(shí)現(xiàn)字典轉(zhuǎn)模型的自動(dòng)轉(zhuǎn)換
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);
//數(shù)據(jù)為數(shù)組時(shí)
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;
}
//數(shù)據(jù)為字典時(shí)
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) {
// 添加成功:說(shuō)明源方法m1現(xiàn)在的實(shí)現(xiàn)為交換方法m2的實(shí)現(xiàn)戚篙,現(xiàn)在將源方法m1的實(shí)現(xiàn)替換到交換方法m2中
class_replaceMethod([self class], @selector(wxs_viewWillAppear:), method_getImplementation(m1), method_getTypeEncoding(m1));
}else {
//添加失敗:說(shuō)明源方法已經(jīng)有實(shí)現(xiàn)臣镣,直接將兩個(gè)方法的實(shí)現(xiàn)交換即
method_exchangeImplementations(m1, m2);
}
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(@"viewWillAppear");
}
- (void)wxs_viewWillAppear:(BOOL)animated {
NSLog(@"Hook : 攔截到viewwillApear的實(shí)現(xiàn)智亮,在其基礎(chǔ)上添加了這行代碼");
[self wxs_viewWillAppear:YES];
}