-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject+SLRuntime.m
More file actions
executable file
·153 lines (96 loc) · 3.5 KB
/
NSObject+SLRuntime.m
File metadata and controls
executable file
·153 lines (96 loc) · 3.5 KB
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
150
151
152
153
//
// NSObject+SLRuntime.h
//
// Created by 秦-政 on 2016/08/16.
//
#import "NSObject+SLRuntime.h"
#import <objc/runtime.h>
@implementation NSObject (SLRuntime)
+ (NSArray *)sl_objectsWithArray:(NSArray *)array {
if (array.count == 0) {
return nil;
}
NSAssert([array[0] isKindOfClass:[NSDictionary class]], @"必须传入字典数组");
NSArray *list = [self sl_propertiesList];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
id obj = [self new];
for (NSString *key in dict) {
if (![list containsObject:key]) {
continue;
}
[obj setValue:dict[key] forKey:key];
}
[arrayM addObject:obj];
}
return arrayM.copy;
}
void *propertiesKey = "cn.itcast.propertiesList";
+ (NSArray *)sl_propertiesList {
// 获取关联对象
NSArray *result = objc_getAssociatedObject(self, propertiesKey);
if (result != nil) {
return result;
}
unsigned int count = 0;
objc_property_t *list = class_copyPropertyList([self class], &count);
NSMutableArray *arrayM = [NSMutableArray array];
for (unsigned int i = 0; i < count; i++) {
objc_property_t pty = list[i];
const char *cName = property_getName(pty);
NSString *name = [NSString stringWithUTF8String:cName];
[arrayM addObject:name];
}
free(list);
// 设置关联对象
objc_setAssociatedObject(self, propertiesKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC);
return objc_getAssociatedObject(self, propertiesKey);
}
void *ivarsKey = "cn.itcast.ivarsList";
+ (NSArray *)sl_ivarsList {
// 获取关联对象
NSArray *result = objc_getAssociatedObject(self, ivarsKey);
if (result != nil) {
return result;
}
unsigned int count = 0;
Ivar *list = class_copyIvarList([self class], &count);
NSMutableArray *arrayM = [NSMutableArray array];
for (unsigned int i = 0; i < count; i++) {
Ivar ivar = list[i];
const char *cName = ivar_getName(ivar);
NSString *name = [NSString stringWithUTF8String:cName];
[arrayM addObject:name];
}
free(list);
// 设置关联对象
objc_setAssociatedObject(self, ivarsKey, arrayM, OBJC_ASSOCIATION_COPY_NONATOMIC);
return objc_getAssociatedObject(self, ivarsKey);
}
+ (NSArray *)sl_methodList {
unsigned int count = 0;
Method *list = class_copyMethodList([self class], &count);
NSMutableArray *arrayM = [NSMutableArray array];
for (unsigned int i = 0; i < count; i++) {
Method method = list[i];
SEL selector = method_getName(method);
NSString *name = NSStringFromSelector(selector);
[arrayM addObject:name];
}
free(list);
return arrayM.copy;
}
+ (NSArray *)sl_protocolList {
unsigned int count = 0;
__unsafe_unretained Protocol **list = class_copyProtocolList([self class], &count);
NSMutableArray *arrayM = [NSMutableArray array];
for (unsigned int i = 0; i < count; i++) {
Protocol *protocol = list[i];
const char *cName = protocol_getName(protocol);
NSString *name = [NSString stringWithUTF8String:cName];
[arrayM addObject:name];
}
free(list);
return arrayM.copy;
}
@end