NSObject+MJProperty.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // NSObject+MJProperty.m
  3. // MJExtensionExample
  4. //
  5. // Created by MJ Lee on 15/4/17.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "NSObject+MJProperty.h"
  9. #import "NSObject+MJKeyValue.h"
  10. #import "NSObject+MJCoding.h"
  11. #import "NSObject+MJClass.h"
  12. #import "MJProperty.h"
  13. #import "MJFoundation.h"
  14. #import <objc/runtime.h>
  15. #pragma clang diagnostic push
  16. #pragma clang diagnostic ignored "-Wundeclared-selector"
  17. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  18. static const char MJReplacedKeyFromPropertyNameKey = '\0';
  19. static const char MJReplacedKeyFromPropertyName121Key = '\0';
  20. static const char MJNewValueFromOldValueKey = '\0';
  21. static const char MJObjectClassInArrayKey = '\0';
  22. static const char MJCachedPropertiesKey = '\0';
  23. @implementation NSObject (Property)
  24. + (NSMutableDictionary *)mj_propertyDictForKey:(const void *)key
  25. {
  26. static NSMutableDictionary *replacedKeyFromPropertyNameDict;
  27. static NSMutableDictionary *replacedKeyFromPropertyName121Dict;
  28. static NSMutableDictionary *newValueFromOldValueDict;
  29. static NSMutableDictionary *objectClassInArrayDict;
  30. static NSMutableDictionary *cachedPropertiesDict;
  31. static dispatch_once_t onceToken;
  32. dispatch_once(&onceToken, ^{
  33. replacedKeyFromPropertyNameDict = [NSMutableDictionary dictionary];
  34. replacedKeyFromPropertyName121Dict = [NSMutableDictionary dictionary];
  35. newValueFromOldValueDict = [NSMutableDictionary dictionary];
  36. objectClassInArrayDict = [NSMutableDictionary dictionary];
  37. cachedPropertiesDict = [NSMutableDictionary dictionary];
  38. });
  39. if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict;
  40. if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict;
  41. if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict;
  42. if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict;
  43. if (key == &MJCachedPropertiesKey) return cachedPropertiesDict;
  44. return nil;
  45. }
  46. #pragma mark - --私有方法--
  47. + (id)mj_propertyKey:(NSString *)propertyName
  48. {
  49. MJExtensionAssertParamNotNil2(propertyName, nil);
  50. __block id key = nil;
  51. // 查看有没有需要替换的key
  52. if ([self respondsToSelector:@selector(mj_replacedKeyFromPropertyName121:)]) {
  53. key = [self mj_replacedKeyFromPropertyName121:propertyName];
  54. }
  55. // 调用block
  56. if (!key) {
  57. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  58. MJReplacedKeyFromPropertyName121 block = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyName121Key);
  59. if (block) {
  60. key = block(propertyName);
  61. }
  62. if (key) *stop = YES;
  63. }];
  64. }
  65. // 查看有没有需要替换的key
  66. if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(mj_replacedKeyFromPropertyName)]) {
  67. key = [self mj_replacedKeyFromPropertyName][propertyName];
  68. }
  69. if (!key || [key isEqual:propertyName]) {
  70. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  71. NSDictionary *dict = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyNameKey);
  72. if (dict) {
  73. key = dict[propertyName];
  74. }
  75. if (key && ![key isEqual:propertyName]) *stop = YES;
  76. }];
  77. }
  78. // 2.用属性名作为key
  79. if (!key) key = propertyName;
  80. return key;
  81. }
  82. + (Class)mj_propertyObjectClassInArray:(NSString *)propertyName
  83. {
  84. __block id clazz = nil;
  85. if ([self respondsToSelector:@selector(mj_objectClassInArray)]) {
  86. clazz = [self mj_objectClassInArray][propertyName];
  87. }
  88. if (!clazz) {
  89. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  90. NSDictionary *dict = objc_getAssociatedObject(c, &MJObjectClassInArrayKey);
  91. if (dict) {
  92. clazz = dict[propertyName];
  93. }
  94. if (clazz) *stop = YES;
  95. }];
  96. }
  97. // 如果是NSString类型
  98. if ([clazz isKindOfClass:[NSString class]]) {
  99. clazz = NSClassFromString(clazz);
  100. }
  101. return clazz;
  102. }
  103. #pragma mark - --公共方法--
  104. + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration
  105. {
  106. // 获得成员变量
  107. MJExtensionSemaphoreCreate
  108. MJExtensionSemaphoreWait
  109. NSArray *cachedProperties = [self mj_properties];
  110. MJExtensionSemaphoreSignal
  111. // 遍历成员变量
  112. BOOL stop = NO;
  113. for (MJProperty *property in cachedProperties) {
  114. enumeration(property, &stop);
  115. if (stop) break;
  116. }
  117. }
  118. #pragma mark - 公共方法
  119. + (NSMutableArray *)mj_properties
  120. {
  121. NSMutableArray *cachedProperties = [self mj_propertyDictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)];
  122. if (cachedProperties == nil) {
  123. if (cachedProperties == nil) {
  124. cachedProperties = [NSMutableArray array];
  125. [self mj_enumerateClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  126. // 1.获得所有的成员变量
  127. unsigned int outCount = 0;
  128. objc_property_t *properties = class_copyPropertyList(c, &outCount);
  129. // 2.遍历每一个成员变量
  130. for (unsigned int i = 0; i<outCount; i++) {
  131. MJProperty *property = [MJProperty cachedPropertyWithProperty:properties[i]];
  132. // 过滤掉Foundation框架类里面的属性
  133. if ([MJFoundation isClassFromFoundation:property.srcClass]) continue;
  134. // 过滤掉`hash`, `superclass`, `description`, `debugDescription`
  135. if ([MJFoundation isFromNSObjectProtocolProperty:property.name]) continue;
  136. property.srcClass = c;
  137. [property setOriginKey:[self mj_propertyKey:property.name] forClass:self];
  138. [property setObjectClassInArray:[self mj_propertyObjectClassInArray:property.name] forClass:self];
  139. [cachedProperties addObject:property];
  140. }
  141. // 3.释放内存
  142. free(properties);
  143. }];
  144. [self mj_propertyDictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)] = cachedProperties;
  145. }
  146. }
  147. return cachedProperties;
  148. }
  149. #pragma mark - 新值配置
  150. + (void)mj_setupNewValueFromOldValue:(MJNewValueFromOldValue)newValueFormOldValue
  151. {
  152. objc_setAssociatedObject(self, &MJNewValueFromOldValueKey, newValueFormOldValue, OBJC_ASSOCIATION_COPY_NONATOMIC);
  153. }
  154. + (id)mj_getNewValueFromObject:(__unsafe_unretained id)object oldValue:(__unsafe_unretained id)oldValue property:(MJProperty *__unsafe_unretained)property{
  155. // 如果有实现方法
  156. if ([object respondsToSelector:@selector(mj_newValueFromOldValue:property:)]) {
  157. return [object mj_newValueFromOldValue:oldValue property:property];
  158. }
  159. // 查看静态设置
  160. __block id newValue = oldValue;
  161. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  162. MJNewValueFromOldValue block = objc_getAssociatedObject(c, &MJNewValueFromOldValueKey);
  163. if (block) {
  164. newValue = block(object, oldValue, property);
  165. *stop = YES;
  166. }
  167. }];
  168. return newValue;
  169. }
  170. #pragma mark - array model class配置
  171. + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
  172. {
  173. [self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];
  174. MJExtensionSemaphoreCreate
  175. MJExtensionSemaphoreWait
  176. [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects];
  177. MJExtensionSemaphoreSignal
  178. }
  179. #pragma mark - key配置
  180. + (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
  181. {
  182. [self mj_setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey];
  183. MJExtensionSemaphoreCreate
  184. MJExtensionSemaphoreWait
  185. [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects];
  186. MJExtensionSemaphoreSignal
  187. }
  188. + (void)mj_setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121
  189. {
  190. objc_setAssociatedObject(self, &MJReplacedKeyFromPropertyName121Key, replacedKeyFromPropertyName121, OBJC_ASSOCIATION_COPY_NONATOMIC);
  191. MJExtensionSemaphoreCreate
  192. MJExtensionSemaphoreWait
  193. [[self mj_propertyDictForKey:&MJCachedPropertiesKey] removeAllObjects];
  194. MJExtensionSemaphoreSignal
  195. }
  196. @end
  197. #pragma clang diagnostic pop