- 對于bug的修復來說,最煩惱的就是在眾多界面中找到對應的viewController,因此耗費了大量時間。我們只需要在控制臺打印出相應的控制器名就能幫我們找到對應的視圖控制器了,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.動態(tài)的根據后臺的json數據修改native用戶模型數據(通過runtime遍歷一個類的全部屬性然后用KVC賦值)
//后臺傳入json字典 native傳入要改變的模型對象 返回改變完屬性的對象
+ (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;
}