- 對(duì)于bug的修復(fù)來說,最煩惱的就是在眾多界面中找到對(duì)應(yīng)的viewController,因此耗費(fèi)了大量時(shí)間藕帜。我們只需要在控制臺(tái)打印出相應(yīng)的控制器名就能幫我們找到對(duì)應(yīng)的視圖控制器了,runtime就能幫到我們了
//
// UIViewController+Swizzling.h
// YL-Health-RB
//
// Created by Alex on 16/10/25.
// Copyright ? 2016年 PingAn. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIViewController (Swizzling)
@end
//
// UIViewController+Swizzling.m
// YL-Health-RB
//
// Created by Alex on 16/10/25.
// Copyright ? 2016年 PingAn. All rights reserved.
//
#import "UIViewController+Swizzling.h"
#import <objc/runtime.h>
@implementation UIViewController (Swizzling)
+ (void)load
{
#ifdef DEBUG
Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:));
method_exchangeImplementations(viewWillAppear, logViewWillAppear);
#endif
}
- (void)logViewWillAppear:(BOOL)animated
{
NSLog(@"========>%@ will appear",NSStringFromClass([self class]));
[self logViewWillAppear:animated];
}
@end
2.動(dòng)態(tài)的根據(jù)后臺(tái)的json數(shù)據(jù)修改native用戶模型數(shù)據(jù)(通過runtime遍歷一個(gè)類的全部屬性然后用KVC賦值)
//后臺(tái)傳入json字典 native傳入要改變的模型對(duì)象 返回改變完屬性的對(duì)象
+ (id)changeObjectPropreties:(id)object dataDic:(NSDictionary *)dic
{
unsigned int count;
NSMutableArray *propertiesArray = [[NSMutableArray alloc]init];
objc_property_t *properties = class_copyPropertyList([object class], &count);
for(int i = 0; i < count; i++)
{
objc_property_t property = properties[i];
NSString *keyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
[propertiesArray addObject:keyName];
}
free(properties);
for (int i = 0; i < [dic allKeys].count; i++) {
NSString *key = [[dic allKeys]objectAtIndex:i];
if ([propertiesArray containsObject:key]) {
[object setValue:[dic objectForKey:key] forKey:key];
}
}
return object;
}