-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestObject.m
47 lines (36 loc) · 922 Bytes
/
TestObject.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// TestObject.m
// objc-test
//
// Created by 孙春磊 on 2020/3/4.
//
#import "TestObject.h"
#import <objc/runtime.h>
@implementation TestObject
+ (void)load{
NSLog(@"%s",__func__);
}
+ (void)initialize{
NSLog(@"%s",__func__);
}
// OC方法
- (void)other
{
NSLog(@"%s", __func__);
NSObject *obj = [NSObject new];
}
/// 动态解析
+ (BOOL)resolveInstanceMethod:(SEL)sel{
if (sel == @selector(unknowSel)) {
// 获取其他方法
Method method = class_getInstanceMethod(self, @selector(other));
// 动态添加test方法的实现 会添加到 class_rw_t中的方法列表中
class_addMethod(self, sel,
method_getImplementation(method),
method_getTypeEncoding(method));
// 返��YES代表有动态添加方法
return YES;
}
return [super resolveInstanceMethod:sel];
}
@end