runtime 的幾個(gè)應(yīng)用場(chǎng)景
1.查看成員變量
2.字典轉(zhuǎn)模型
3.方法交換
-
1.查看成員變量01-修改textField展位文本顏色寂诱,給textView添加占位提醒
把textField改成紅色 詳情點(diǎn)這里
-
2.字典轉(zhuǎn)模型
// 假如這是后臺(tái)收到的數(shù)據(jù)
NSDictionary *json = @{
@"tagId" : @45676000,
@"age3" : @2, //這個(gè)是age3极谊,與本地的模型字段不一樣
@"weight" : @10.3,
@"name" : @"wangcai"
};
JFDog *dog = [JFDog jf_objectWithJson:json];
NSLog(@"\n--tagid:%ld\n--age:%ld\n--weight:%f\n--name:%@\n",dog.tagId,dog.age, dog.weight, dog.name);
//本地模型類
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface JFDog : NSObject
@property (assign, nonatomic) NSInteger tagId;
@property (assign, nonatomic) CGFloat weight;
@property (assign, nonatomic) NSInteger age;
@property (copy, nonatomic) NSString *name;
@end
NS_ASSUME_NONNULL_END
//寫個(gè)NSObject分類租谈,實(shí)現(xiàn)字典轉(zhuǎn)模型
#import "NSObject+JFJson.h"
#import <objc/runtime.h>
@implementation NSObject (JFJson)
+ (instancetype)jf_objectWithJson:(NSDictionary *)json{
id obj = [[self alloc] init];
unsigned int count;
Ivar *ivars = class_copyIvarList(self, &count);
for (int i = 0; i < count; i++) {
// 取出i位置的成員變量
Ivar ivar = ivars[I];
NSMutableString *name = [NSMutableString stringWithUTF8String:ivar_getName(ivar)];
[name deleteCharactersInRange:NSMakeRange(0, 1)];//刪除取出的_字符
// 設(shè)值
id value = json[name];
if ([name isEqualToString:@"age"]) {//處理本地與網(wǎng)絡(luò)字符不匹配的問(wèn)題
value = json[@"age3"];
}
[obj setValue:value forKey:name];
}
free(ivars);
return obj;
}
@end
//這只是幫助理解字典轉(zhuǎn)模型庫(kù)的基本實(shí)現(xiàn)捏检,離真正意義上的字典轉(zhuǎn)模型庫(kù)差的還很遠(yuǎn)轧粟,模型嵌套了供填,復(fù)雜數(shù)據(jù)結(jié)果等一大堆問(wèn)題需要處理赡模。
-
3.方法交換01-掃盲
方法交換Method Swizzling
又叫黑魔法,
#import "ViewController.h"
#import "JFDog.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
JFDog *dog = [JFDog new];
[dog eat];
}
@end
///JFDog.h
#import <Foundation/Foundation.h>
@interface JFDog : NSObject
- (void)eat;
-(void)play;
+(void)play2;
@end
#import "JFDog.h"
#import <objc/runtime.h>
@implementation JFDog
- (void)eat{
NSLog(@"dog--eat");
}
-(void)play{
NSLog(@"dog--play");
}
+(void)play2{
NSLog(@"classFunc--dog--play");
}
///load 會(huì)在類或者類的分類添加到 Objective-c runtime 時(shí)調(diào)用查吊,
///父類的 +load 方法先于子類的 +load 方法調(diào)用谐区,
///分類的 +load 方法先于類本身的 +load 方法調(diào)用。
+(void)load{
Method method1 = class_getInstanceMethod(self.class, @selector(eat));
Method method2 = class_getInstanceMethod(self.class, @selector(play));
// Method method3 = class_getClassMethod(self, @selector(play2)); //當(dāng)然你也可以交換類方法和對(duì)象方法的實(shí)現(xiàn)
//進(jìn)行交換
method_exchangeImplementations(method1, method2);
}
@end
///這會(huì)打印dog--play逻卖,說(shuō)明方法交換成功了
-
3.方法交換02-hook
//hook翻譯是鉤子宋列,吧自己的方法掛載在系統(tǒng)方法中,
//理念就是评也,先攔截住系統(tǒng)方法炼杖,加入處理方案,然后再轉(zhuǎn)到系統(tǒng)方法里
需求:數(shù)組添加空對(duì)象不會(huì)崩潰
//在main函數(shù)里創(chuàng)建一個(gè)數(shù)組調(diào)用
NSString *obj = nil;
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"jack"];
[array insertObject:obj atIndex:0];
//NSMutableArray寫個(gè)分類
#import "NSMutableArray+Extension.h"
#import <objc/runtime.h>
@implementation NSMutableArray (Extension)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 類簇:NSString盗迟、NSArray坤邪、NSDictionary,真實(shí)類型是其他類型
Class cls = NSClassFromString(@"__NSArrayM");
Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));
Method method2 = class_getInstanceMethod(cls, @selector(jf_insertObject:atIndex:));
method_exchangeImplementations(method1, method2);
});
}
- (void)jf_insertObject:(id)anObject atIndex:(NSUInteger)index{
if (anObject == nil) return;//如果是空對(duì)象罚缕,就不添加艇纺,防止奔潰
//攔截之后,再調(diào)用系統(tǒng)的實(shí)現(xiàn)怕磨,由于不知道底層實(shí)現(xiàn)邏輯喂饥,自己實(shí)現(xiàn)系統(tǒng)方法可能會(huì)出各種意想不到的錯(cuò)
[self jf_insertObject:anObject atIndex:index];//這兒看似死循環(huán),細(xì)理邏輯肠鲫,方法實(shí)現(xiàn)已經(jīng)交換過(guò)了的
}
@end