NSObject+MJProperty.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. static NSMutableDictionary *replacedKeyFromPropertyNameDict_;
  25. static NSMutableDictionary *replacedKeyFromPropertyName121Dict_;
  26. static NSMutableDictionary *newValueFromOldValueDict_;
  27. static NSMutableDictionary *objectClassInArrayDict_;
  28. static NSMutableDictionary *cachedPropertiesDict_;
  29. + (void)load
  30. {
  31. replacedKeyFromPropertyNameDict_ = [NSMutableDictionary dictionary];
  32. replacedKeyFromPropertyName121Dict_ = [NSMutableDictionary dictionary];
  33. newValueFromOldValueDict_ = [NSMutableDictionary dictionary];
  34. objectClassInArrayDict_ = [NSMutableDictionary dictionary];
  35. cachedPropertiesDict_ = [NSMutableDictionary dictionary];
  36. }
  37. + (NSMutableDictionary *)dictForKey:(const void *)key
  38. {
  39. @synchronized (self) {
  40. if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_;
  41. if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_;
  42. if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_;
  43. if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_;
  44. if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_;
  45. return nil;
  46. }
  47. }
  48. #pragma mark - --私有方法--
  49. + (id)propertyKey:(NSString *)propertyName
  50. {
  51. MJExtensionAssertParamNotNil2(propertyName, nil);
  52. __block id key = nil;
  53. // 查看有没有需要替换的key
  54. if ([self respondsToSelector:@selector(mj_replacedKeyFromPropertyName121:)]) {
  55. key = [self mj_replacedKeyFromPropertyName121:propertyName];
  56. }
  57. // 兼容旧版本
  58. if ([self respondsToSelector:@selector(replacedKeyFromPropertyName121:)]) {
  59. key = [self performSelector:@selector(replacedKeyFromPropertyName121) withObject:propertyName];
  60. }
  61. // 调用block
  62. if (!key) {
  63. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  64. MJReplacedKeyFromPropertyName121 block = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyName121Key);
  65. if (block) {
  66. key = block(propertyName);
  67. }
  68. if (key) *stop = YES;
  69. }];
  70. }
  71. // 查看有没有需要替换的key
  72. if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(mj_replacedKeyFromPropertyName)]) {
  73. key = [self mj_replacedKeyFromPropertyName][propertyName];
  74. }
  75. // 兼容旧版本
  76. if ((!key || [key isEqual:propertyName]) && [self respondsToSelector:@selector(replacedKeyFromPropertyName)]) {
  77. key = [self performSelector:@selector(replacedKeyFromPropertyName)][propertyName];
  78. }
  79. if (!key || [key isEqual:propertyName]) {
  80. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  81. NSDictionary *dict = objc_getAssociatedObject(c, &MJReplacedKeyFromPropertyNameKey);
  82. if (dict) {
  83. key = dict[propertyName];
  84. }
  85. if (key && ![key isEqual:propertyName]) *stop = YES;
  86. }];
  87. }
  88. // 2.用属性名作为key
  89. if (!key) key = propertyName;
  90. return key;
  91. }
  92. + (Class)propertyObjectClassInArray:(NSString *)propertyName
  93. {
  94. __block id clazz = nil;
  95. if ([self respondsToSelector:@selector(mj_objectClassInArray)]) {
  96. clazz = [self mj_objectClassInArray][propertyName];
  97. }
  98. // 兼容旧版本
  99. if ([self respondsToSelector:@selector(objectClassInArray)]) {
  100. clazz = [self performSelector:@selector(objectClassInArray)][propertyName];
  101. }
  102. if (!clazz) {
  103. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  104. NSDictionary *dict = objc_getAssociatedObject(c, &MJObjectClassInArrayKey);
  105. if (dict) {
  106. clazz = dict[propertyName];
  107. }
  108. if (clazz) *stop = YES;
  109. }];
  110. }
  111. // 如果是NSString类型
  112. if ([clazz isKindOfClass:[NSString class]]) {
  113. clazz = NSClassFromString(clazz);
  114. }
  115. return clazz;
  116. }
  117. #pragma mark - --公共方法--
  118. + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration
  119. {
  120. // 获得成员变量
  121. NSArray *cachedProperties = [self properties];
  122. // 遍历成员变量
  123. BOOL stop = NO;
  124. for (MJProperty *property in cachedProperties) {
  125. enumeration(property, &stop);
  126. if (stop) break;
  127. }
  128. }
  129. #pragma mark - 公共方法
  130. + (NSMutableArray *)properties
  131. {
  132. NSMutableArray *cachedProperties = [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)];
  133. if (cachedProperties == nil) {
  134. cachedProperties = [NSMutableArray array];
  135. [self mj_enumerateClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  136. // 1.获得所有的成员变量
  137. unsigned int outCount = 0;
  138. objc_property_t *properties = class_copyPropertyList(c, &outCount);
  139. // 2.遍历每一个成员变量
  140. for (unsigned int i = 0; i<outCount; i++) {
  141. MJProperty *property = [MJProperty cachedPropertyWithProperty:properties[i]];
  142. // 过滤掉Foundation框架类里面的属性
  143. if ([MJFoundation isClassFromFoundation:property.srcClass]) continue;
  144. property.srcClass = c;
  145. [property setOriginKey:[self propertyKey:property.name] forClass:self];
  146. [property setObjectClassInArray:[self propertyObjectClassInArray:property.name] forClass:self];
  147. [cachedProperties addObject:property];
  148. }
  149. // 3.释放内存
  150. free(properties);
  151. }];
  152. [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)] = cachedProperties;
  153. }
  154. return cachedProperties;
  155. }
  156. #pragma mark - 新值配置
  157. + (void)mj_setupNewValueFromOldValue:(MJNewValueFromOldValue)newValueFormOldValue
  158. {
  159. objc_setAssociatedObject(self, &MJNewValueFromOldValueKey, newValueFormOldValue, OBJC_ASSOCIATION_COPY_NONATOMIC);
  160. }
  161. + (id)mj_getNewValueFromObject:(__unsafe_unretained id)object oldValue:(__unsafe_unretained id)oldValue property:(MJProperty *__unsafe_unretained)property{
  162. // 如果有实现方法
  163. if ([object respondsToSelector:@selector(mj_newValueFromOldValue:property:)]) {
  164. return [object mj_newValueFromOldValue:oldValue property:property];
  165. }
  166. // 兼容旧版本
  167. if ([self respondsToSelector:@selector(newValueFromOldValue:property:)]) {
  168. return [self performSelector:@selector(newValueFromOldValue:property:) withObject:oldValue withObject:property];
  169. }
  170. // 查看静态设置
  171. __block id newValue = oldValue;
  172. [self mj_enumerateAllClasses:^(__unsafe_unretained Class c, BOOL *stop) {
  173. MJNewValueFromOldValue block = objc_getAssociatedObject(c, &MJNewValueFromOldValueKey);
  174. if (block) {
  175. newValue = block(object, oldValue, property);
  176. *stop = YES;
  177. }
  178. }];
  179. return newValue;
  180. }
  181. #pragma mark - array model class配置
  182. + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
  183. {
  184. [self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];
  185. [[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
  186. }
  187. #pragma mark - key配置
  188. + (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
  189. {
  190. [self mj_setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey];
  191. [[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
  192. }
  193. + (void)mj_setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121
  194. {
  195. objc_setAssociatedObject(self, &MJReplacedKeyFromPropertyName121Key, replacedKeyFromPropertyName121, OBJC_ASSOCIATION_COPY_NONATOMIC);
  196. [[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
  197. }
  198. @end
  199. @implementation NSObject (MJPropertyDeprecated_v_2_5_16)
  200. + (void)enumerateProperties:(MJPropertiesEnumeration)enumeration
  201. {
  202. [self mj_enumerateProperties:enumeration];
  203. }
  204. + (void)setupNewValueFromOldValue:(MJNewValueFromOldValue)newValueFormOldValue
  205. {
  206. [self mj_setupNewValueFromOldValue:newValueFormOldValue];
  207. }
  208. + (id)getNewValueFromObject:(__unsafe_unretained id)object oldValue:(__unsafe_unretained id)oldValue property:(__unsafe_unretained MJProperty *)property
  209. {
  210. return [self mj_getNewValueFromObject:object oldValue:oldValue property:property];
  211. }
  212. + (void)setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
  213. {
  214. [self mj_setupReplacedKeyFromPropertyName:replacedKeyFromPropertyName];
  215. }
  216. + (void)setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121
  217. {
  218. [self mj_setupReplacedKeyFromPropertyName121:replacedKeyFromPropertyName121];
  219. }
  220. + (void)setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
  221. {
  222. [self mj_setupObjectClassInArray:objectClassInArray];
  223. }
  224. @end
  225. #pragma clang diagnostic pop