1717static const char MJAllowedCodingPropertyNamesKey = ' \0 ' ;
1818static const char MJIgnoredCodingPropertyNamesKey = ' \0 ' ;
1919
20- static NSMutableDictionary *allowedPropertyNamesDict_;
21- static NSMutableDictionary *ignoredPropertyNamesDict_;
22- static NSMutableDictionary *allowedCodingPropertyNamesDict_;
23- static NSMutableDictionary *ignoredCodingPropertyNamesDict_;
24-
2520@implementation NSObject (MJClass)
2621
27- + (void )load
28- {
29- allowedPropertyNamesDict_ = [NSMutableDictionary dictionary ];
30- ignoredPropertyNamesDict_ = [NSMutableDictionary dictionary ];
31- allowedCodingPropertyNamesDict_ = [NSMutableDictionary dictionary ];
32- ignoredCodingPropertyNamesDict_ = [NSMutableDictionary dictionary ];
33- }
34-
3522+ (NSMutableDictionary *)dictForKey : (const void *)key
3623{
37- @synchronized (self) {
38- if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_;
39- if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_;
40- if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_;
41- if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_;
42- return nil ;
43- }
24+ static NSMutableDictionary *allowedPropertyNamesDict;
25+ static NSMutableDictionary *ignoredPropertyNamesDict;
26+ static NSMutableDictionary *allowedCodingPropertyNamesDict;
27+ static NSMutableDictionary *ignoredCodingPropertyNamesDict;
28+
29+ static dispatch_once_t onceToken;
30+ dispatch_once (&onceToken, ^{
31+ allowedPropertyNamesDict = [NSMutableDictionary dictionary ];
32+ ignoredPropertyNamesDict = [NSMutableDictionary dictionary ];
33+ allowedCodingPropertyNamesDict = [NSMutableDictionary dictionary ];
34+ ignoredCodingPropertyNamesDict = [NSMutableDictionary dictionary ];
35+ });
36+
37+ if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict;
38+ if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict;
39+ if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict;
40+ if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict;
41+ return nil ;
4442}
4543
4644+ (void )mj_enumerateClasses : (MJClassesEnumeration)enumeration
@@ -130,6 +128,7 @@ + (NSMutableArray *)mj_totalAllowedCodingPropertyNames
130128{
131129 return [self mj_totalObjectsWithSelector: @selector (mj_allowedCodingPropertyNames ) key: &MJAllowedCodingPropertyNamesKey];
132130}
131+
133132#pragma mark - block和方法处理:存储block的返回值
134133+ (void )mj_setupBlockReturnValue : (id (^)())block key : (const char *)key
135134{
@@ -145,26 +144,32 @@ + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key
145144
146145+ (NSMutableArray *)mj_totalObjectsWithSelector : (SEL )selector key : (const char *)key
147146{
148- NSMutableArray *array = [self dictForKey: key][NSStringFromClass (self )];
149- if (array) return array;
150-
151- // 创建、存储
152- [self dictForKey: key][NSStringFromClass (self )] = array = [NSMutableArray array ];
147+ MJExtensionSemaphoreCreate
148+ MJExtensionSemaphoreWait
153149
154- if ([self respondsToSelector: selector]) {
155- #pragma clang diagnostic push
156- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
157- NSArray *subArray = [self performSelector: selector];
158- #pragma clang diagnostic pop
159- if (subArray) {
160- [array addObjectsFromArray: subArray];
150+ NSMutableArray *array = [self dictForKey: key][NSStringFromClass (self )];
151+ if (array == nil ) {
152+ // 创建、存储
153+ [self dictForKey: key][NSStringFromClass (self )] = array = [NSMutableArray array ];
154+
155+ if ([self respondsToSelector: selector]) {
156+ #pragma clang diagnostic push
157+ #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
158+ NSArray *subArray = [self performSelector: selector];
159+ #pragma clang diagnostic pop
160+ if (subArray) {
161+ [array addObjectsFromArray: subArray];
162+ }
161163 }
164+
165+ [self mj_enumerateAllClasses: ^(__unsafe_unretained Class c, BOOL *stop) {
166+ NSArray *subArray = objc_getAssociatedObject (c, key);
167+ [array addObjectsFromArray: subArray];
168+ }];
162169 }
163170
164- [self mj_enumerateAllClasses: ^(__unsafe_unretained Class c, BOOL *stop) {
165- NSArray *subArray = objc_getAssociatedObject (c, key);
166- [array addObjectsFromArray: subArray];
167- }];
171+ MJExtensionSemaphoreSignal
172+
168173 return array;
169174}
170175@end
0 commit comments