在 iOS 開發(fā)中,我們可能會使用 Runtime 來解決問題讶隐,當(dāng)然我們可能都接觸過 Runtime,有的小伙伴已經(jīng)很熟悉了升酣,有的小伙伴也可能不是特別清楚,為了讓小伙伴們更好地使用 Runtime态罪,我對 Runtime 做了一個小小的封裝噩茄,先看一下效果吧。
使用說明
- 導(dǎo)入頭文件
#import "DYFRuntimeWrapper.h"
- Runtime 應(yīng)用介紹
- 獲取所有方法名复颈,例如獲取 UITableView 的方法名:
NSArray *list = [DYFRuntimeWrapper yf_getAllMethodsWithClass:[UITableView class]];
for (NSString *name in list) {
NSLog(@">>>> %@", name);
}
- 獲取所有屬性名绩聘,例如獲取 UILabel 的屬性變量:
NSArray *list = [DYFRuntimeWrapper yf_getAllIvarsWithClass:[UILabel class]];
for (NSString *name in list) {
NSLog(@">>>> %@", name);
}
- 添加一個方法
+ (void)load {
[DYFRuntimeWrapper yf_addMethodWithClass:[self class] methodName:@"hello" impClass:[self class] impName:@"sayHello"];
}
- (void)viewDidLoad {
[super viewDidLoad];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:NSSelectorFromString(@"hello")];
#pragma clang diagnostic pop
}
- (void)sayHello {
NSLog(@">>>> %s", __FUNCTION__);
}
- 交換兩個方法
- (IBAction)exchangeMethod:(id)sender {
[DYFRuntimeWrapper yf_exchangeMethodWithSourceClass:[self class] sourceSel:@selector(preload) targetClass:[self class] targetSel:@selector(refreshUI)];
}
- (void)preload {
NSString *str = [NSString stringWithFormat:@"%s\n", __FUNCTION__];
NSLog(@">>>> str1: %@", str);
self.displayView.text = [self.displayView.text stringByAppendingString:str];
NSLog(@">>>> self.displayView.text: %@", self.displayView.text);
}
- (void)refreshUI {
NSString *str = [NSString stringWithFormat:@"%s-%@\n", __FUNCTION__, self.randomString];
NSLog(@">>>> str2: %@", str);
self.displayView.text = [self.displayView.text stringByAppendingString:str];
NSLog(@">>>> self.displayView.text: %@", self.displayView.text);
}
- (NSString *)randomString {
uint32_t a = arc4random_uniform(2);
unsigned int len = (a == 1) ? 40 : 32;
char data[len];
for (int x = 0; x < len; x++) {
uint32_t ar = arc4random_uniform(2);
char ch = (char)(((ar == 1) ? 'A' : 'a') + (arc4random_uniform(26)));
data[x] = ch;
}
return [[NSString alloc] initWithBytes:data length:len encoding:NSUTF8StringEncoding];
}
- 替換某個方法
- (IBAction)replaceMethod:(id)sender {
[DYFRuntimeWrapper yf_replaceMethodWithSourceClass:[self class] sourceSel:@selector(preload) targetClass:[self class] targetSel:@selector(refreshUI)];
}
// 同上
- 字典轉(zhuǎn)模型
- (IBAction)dictToModel:(id)sender {
NSDictionary *dict = @{@"name": self.nameTF.text,
@"gender": self.genderTF.text,
@"age": self.ageTF.text};
YFModel *model = [DYFRuntimeWrapper yf_modelWithDict:dict modelClass:[YFModel class]];
self.displayView.text = [self.displayView.text stringByAppendingString:[NSString stringWithFormat:@"\nname: %@\ngender: %@\nage: %@", model.name, model.gender, model.age]];
}
- 歸檔解檔
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建路徑
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"YFModel.plist"];
self.filePath = filePath;
}
- (IBAction)archive:(id)sender {
YFModel *model = [YFModel new];
model.name = self.nameTF.text;
model.gender = self.genderTF.text;
model.age = self.ageTF.text;
BOOL ret = [DYFRuntimeWrapper yf_archive:[model class] model:model filePath:self.filePath];
if (ret) {
NSLog(@">>>> 歸檔成功:%@", self.filePath);
} else {
NSLog(@">>>> 歸檔失敗:睦病T淦小!");
}
}
- (IBAction)unarchive:(id)sender {
self.displayView.text = @"解檔后的數(shù)據(jù):";
YFModel *m = [DYFRuntimeWrapper yf_unarchive:[YFModel class] filePath:self.filePath];
self.displayView.text = [self.displayView.text stringByAppendingString:[NSString stringWithFormat:@"\nname: %@\ngender: %@\nage: %@", m.name, m.gender, m.age]];
}
- 添加分類屬性
導(dǎo)入頭文件
#import <objc/message.h>
聲明屬性
/** 居住地址 */
@property (nonatomic, copy) NSString *address;
- 申明一個key值
static NSString *kHomeAddress = @"kHomeAddress";
- 重寫setter帜讲、getter方法
// getter
- (NSString *)address {
return objc_getAssociatedObject(self, &kHomeAddress);
}
// setter
- (void)setAddress:(NSString *)address {
objc_setAssociatedObject(self, &kHomeAddress, address, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
這樣就成功添加了一個分類屬性衅谷。
Runtime 應(yīng)用的封裝主要實現(xiàn)都在 DYFRuntimeWrapper 類中,如果需要快速使用 Runtime似将,就去查看或下載 Demo获黔。
點贊+關(guān)注,第一時間獲取技術(shù)干貨和最新知識點在验,謝謝你的支持玷氏!
最后祝大家生活愉快~