-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNSObjectX.m
149 lines (108 loc) · 3.77 KB
/
NSObjectX.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#import "NSObjectX.h"
#import <objc/runtime.h>
BOOL method_swizzle(Class klass, SEL origSel, SEL altSel)
{
if (!klass)
return NO;
Method __block origMethod, __block altMethod;
void (^find_methods)() = ^
{
unsigned methodCount = 0;
Method *methodList = class_copyMethodList(klass, &methodCount);
origMethod = altMethod = NULL;
if (methodList)
for (unsigned i = 0; i < methodCount; ++i)
{
if (method_getName(methodList[i]) == origSel)
origMethod = methodList[i];
if (method_getName(methodList[i]) == altSel)
altMethod = methodList[i];
}
free(methodList);
};
find_methods();
if (!origMethod)
{
origMethod = class_getInstanceMethod(klass, origSel);
if (!origMethod)
return NO;
if (!class_addMethod(klass, method_getName(origMethod), method_getImplementation(origMethod), method_getTypeEncoding(origMethod)))
return NO;
}
if (!altMethod)
{
altMethod = class_getInstanceMethod(klass, altSel);
if (!altMethod)
return NO;
if (!class_addMethod(klass, method_getName(altMethod), method_getImplementation(altMethod), method_getTypeEncoding(altMethod)))
return NO;
}
find_methods();
if (!origMethod || !altMethod)
return NO;
method_exchangeImplementations(origMethod, altMethod);
return YES;
}
void method_append(Class toClass, Class fromClass, SEL selector)
{
if (!toClass || !fromClass || !selector)
return;
Method method = class_getInstanceMethod(fromClass, selector);
if (!method)
return;
class_addMethod(toClass, method_getName(method), method_getImplementation(method), method_getTypeEncoding(method));
}
void method_replace(Class toClass, Class fromClass, SEL selector)
{
if (!toClass || !fromClass || ! selector)
return;
Method method = class_getInstanceMethod(fromClass, selector);
if (!method)
return;
class_replaceMethod(toClass, method_getName(method), method_getImplementation(method), method_getTypeEncoding(method));
}
@implementation NSObject (X)
+ (void)swizzleMethod:(SEL)originalMethod withMethod:(SEL)newMethod
{
method_swizzle(self.class, originalMethod, newMethod);
}
+ (void)appendMethod:(SEL)newMethod fromClass:(Class)klass
{
method_append(self.class, klass, newMethod);
}
+ (void)replaceMethod:(SEL)method fromClass:(Class)klass
{
method_replace(self.class, klass, method);
}
- (BOOL)respondsToSelector:(SEL)selector untilClass:(Class)stopClass
{
return [self.class instancesRespondToSelector:selector untilClass:stopClass];
}
- (BOOL)superRespondsToSelector:(SEL)selector
{
return [self.superclass instancesRespondToSelector:selector];
}
- (BOOL)superRespondsToSelector:(SEL)selector untilClass:(Class)stopClass
{
return [self.superclass instancesRespondToSelector:selector untilClass:stopClass];
}
+ (BOOL)instancesRespondToSelector:(SEL)selector untilClass:(Class)stopClass
{
BOOL __block (^ __weak block_self)(Class klass, SEL selector, Class stopClass);
BOOL (^block)(Class klass, SEL selector, Class stopClass) = [^
(Class klass, SEL selector, Class stopClass)
{
if (!klass || klass == stopClass)
return NO;
unsigned methodCount = 0;
Method *methodList = class_copyMethodList(klass, &methodCount);
if (methodList)
for (unsigned i = 0; i < methodCount; ++i)
if (method_getName(methodList[i]) == selector)
return YES;
return block_self(klass.superclass, selector, stopClass);
} copy];
block_self = block;
return block(self.class, selector, stopClass);
}
@end