NSObject+MJProperty.m 8.6 KB

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