NSObject+MJProperty.m 10 KB

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