MJProperty.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // MJProperty.m
  3. // MJExtensionExample
  4. //
  5. // Created by MJ Lee on 15/4/17.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJProperty.h"
  9. #import "MJFoundation.h"
  10. #import "MJExtensionConst.h"
  11. #import <objc/message.h>
  12. #include "TargetConditionals.h"
  13. @interface MJProperty()
  14. @property (strong, nonatomic) NSMutableDictionary *propertyKeysDict;
  15. @property (strong, nonatomic) NSMutableDictionary *objectClassInArrayDict;
  16. @property (strong, nonatomic) dispatch_semaphore_t propertyKeysLock;
  17. @property (strong, nonatomic) dispatch_semaphore_t objectClassInArrayLock;
  18. @end
  19. @implementation MJProperty
  20. #pragma mark - 初始化
  21. - (instancetype)init
  22. {
  23. if (self = [super init]) {
  24. _propertyKeysDict = [NSMutableDictionary dictionary];
  25. _objectClassInArrayDict = [NSMutableDictionary dictionary];
  26. _propertyKeysLock = dispatch_semaphore_create(1);
  27. _objectClassInArrayLock = dispatch_semaphore_create(1);
  28. }
  29. return self;
  30. }
  31. #pragma mark - 缓存
  32. + (instancetype)cachedPropertyWithProperty:(objc_property_t)property
  33. {
  34. MJProperty *propertyObj = objc_getAssociatedObject(self, property);
  35. if (propertyObj == nil) {
  36. propertyObj = [[self alloc] init];
  37. propertyObj.property = property;
  38. objc_setAssociatedObject(self, property, propertyObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  39. }
  40. return propertyObj;
  41. }
  42. #pragma mark - 公共方法
  43. - (void)setProperty:(objc_property_t)property
  44. {
  45. _property = property;
  46. MJExtensionAssertParamNotNil(property);
  47. // 1.属性名
  48. _name = @(property_getName(property));
  49. // 2.成员类型
  50. NSString *attrs = @(property_getAttributes(property));
  51. NSUInteger dotLoc = [attrs rangeOfString:@","].location;
  52. NSString *code = nil;
  53. NSUInteger loc = 1;
  54. if (dotLoc == NSNotFound) { // 没有,
  55. code = [attrs substringFromIndex:loc];
  56. } else {
  57. code = [attrs substringWithRange:NSMakeRange(loc, dotLoc - loc)];
  58. }
  59. _type = [MJPropertyType cachedTypeWithCode:code];
  60. }
  61. /**
  62. * 获得成员变量的值
  63. */
  64. - (id)valueForObject:(id)object
  65. {
  66. if (self.type.KVCDisabled) return [NSNull null];
  67. id value = [object valueForKey:self.name];
  68. // 32位BOOL类型转换json后成Int类型
  69. /** https://github.com/CoderMJLee/MJExtension/issues/545 */
  70. // 32 bit device OR 32 bit Simulator
  71. #if defined(__arm__) || (TARGET_OS_SIMULATOR && !__LP64__)
  72. if (self.type.isBoolType) {
  73. value = @([(NSNumber *)value boolValue]);
  74. }
  75. #endif
  76. return value;
  77. }
  78. /**
  79. * 设置成员变量的值
  80. */
  81. - (void)setValue:(id)value forObject:(id)object
  82. {
  83. if (self.type.KVCDisabled || value == nil) return;
  84. [object setValue:value forKey:self.name];
  85. }
  86. /**
  87. * 通过字符串key创建对应的keys
  88. */
  89. - (NSArray *)propertyKeysWithStringKey:(NSString *)stringKey
  90. {
  91. if (stringKey.length == 0) return nil;
  92. NSMutableArray *propertyKeys = [NSMutableArray array];
  93. // 如果有多级映射
  94. NSArray *oldKeys = [stringKey componentsSeparatedByString:@"."];
  95. for (NSString *oldKey in oldKeys) {
  96. NSUInteger start = [oldKey rangeOfString:@"["].location;
  97. if (start != NSNotFound) { // 有索引的key
  98. NSString *prefixKey = [oldKey substringToIndex:start];
  99. NSString *indexKey = prefixKey;
  100. if (prefixKey.length) {
  101. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  102. propertyKey.name = prefixKey;
  103. [propertyKeys addObject:propertyKey];
  104. indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
  105. }
  106. /** 解析索引 **/
  107. // 元素
  108. NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
  109. for (NSInteger i = 0; i<cmps.count - 1; i++) {
  110. MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
  111. subPropertyKey.type = MJPropertyKeyTypeArray;
  112. subPropertyKey.name = cmps[i];
  113. [propertyKeys addObject:subPropertyKey];
  114. }
  115. } else { // 没有索引的key
  116. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  117. propertyKey.name = oldKey;
  118. [propertyKeys addObject:propertyKey];
  119. }
  120. }
  121. return propertyKeys;
  122. }
  123. /** 对应着字典中的key */
  124. - (void)setOriginKey:(id)originKey forClass:(Class)c
  125. {
  126. if ([originKey isKindOfClass:[NSString class]]) { // 字符串类型的key
  127. NSArray *propertyKeys = [self propertyKeysWithStringKey:originKey];
  128. if (propertyKeys.count) {
  129. [self setPropertyKeys:@[propertyKeys] forClass:c];
  130. }
  131. } else if ([originKey isKindOfClass:[NSArray class]]) {
  132. NSMutableArray *keyses = [NSMutableArray array];
  133. for (NSString *stringKey in originKey) {
  134. NSArray *propertyKeys = [self propertyKeysWithStringKey:stringKey];
  135. if (propertyKeys.count) {
  136. [keyses addObject:propertyKeys];
  137. }
  138. }
  139. if (keyses.count) {
  140. [self setPropertyKeys:keyses forClass:c];
  141. }
  142. }
  143. }
  144. /** 对应着字典中的多级key */
  145. - (void)setPropertyKeys:(NSArray *)propertyKeys forClass:(Class)c
  146. {
  147. if (propertyKeys.count == 0) return;
  148. NSString *key = NSStringFromClass(c);
  149. if (!key) return;
  150. MJ_LOCK(self.propertyKeysLock);
  151. self.propertyKeysDict[key] = propertyKeys;
  152. MJ_UNLOCK(self.propertyKeysLock);
  153. }
  154. - (NSArray *)propertyKeysForClass:(Class)c
  155. {
  156. NSString *key = NSStringFromClass(c);
  157. if (!key) return nil;
  158. MJ_LOCK(self.propertyKeysLock);
  159. NSArray *propertyKeys = self.propertyKeysDict[key];
  160. MJ_UNLOCK(self.propertyKeysLock);
  161. return propertyKeys;
  162. }
  163. /** 模型数组中的模型类型 */
  164. - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c
  165. {
  166. if (!objectClass) return;
  167. NSString *key = NSStringFromClass(c);
  168. if (!key) return;
  169. MJ_LOCK(self.objectClassInArrayLock);
  170. self.objectClassInArrayDict[key] = objectClass;
  171. MJ_UNLOCK(self.objectClassInArrayLock);
  172. }
  173. - (Class)objectClassInArrayForClass:(Class)c
  174. {
  175. NSString *key = NSStringFromClass(c);
  176. if (!key) return nil;
  177. MJ_LOCK(self.objectClassInArrayLock);
  178. Class objectClass = self.objectClassInArrayDict[key];
  179. MJ_UNLOCK(self.objectClassInArrayLock);
  180. return objectClass;
  181. }
  182. @end