NSObject+MJProperty.m 8.6 KB

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