MJProperty.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // NSLog(@"32位BOOL类型转换json后成Int类型 %@",[object mj_keyValues]);
  68. id value=@"";//userId
  69. @try {
  70. value = [object valueForKey:_name];
  71. } @catch (NSException *exception) {
  72. } @finally {
  73. }
  74. // 32位BOOL类型转换json后成Int类型
  75. /** https://github.com/CoderMJLee/MJExtension/issues/545 */
  76. // 32 bit device OR 32 bit Simulator
  77. #if defined(__arm__) || (TARGET_OS_SIMULATOR && !__LP64__)
  78. if (self.type.isBoolType) {
  79. value = @([(NSNumber *)value boolValue]);
  80. }
  81. #endif
  82. return value;
  83. }
  84. /**
  85. * 设置成员变量的值
  86. */
  87. - (void)setValue:(id)value forObject:(id)object
  88. {
  89. if (self.type.KVCDisabled || value == nil) return;
  90. [object setValue:value forKey:self.name];
  91. }
  92. /**
  93. * 通过字符串key创建对应的keys
  94. */
  95. - (NSArray *)propertyKeysWithStringKey:(NSString *)stringKey
  96. {
  97. if (stringKey.length == 0) return nil;
  98. NSMutableArray *propertyKeys = [NSMutableArray array];
  99. // 如果有多级映射
  100. NSArray *oldKeys = [stringKey componentsSeparatedByString:@"."];
  101. for (NSString *oldKey in oldKeys) {
  102. NSUInteger start = [oldKey rangeOfString:@"["].location;
  103. if (start != NSNotFound) { // 有索引的key
  104. NSString *prefixKey = [oldKey substringToIndex:start];
  105. NSString *indexKey = prefixKey;
  106. if (prefixKey.length) {
  107. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  108. propertyKey.name = prefixKey;
  109. [propertyKeys addObject:propertyKey];
  110. indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
  111. }
  112. /** 解析索引 **/
  113. // 元素
  114. NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
  115. for (NSInteger i = 0; i<cmps.count - 1; i++) {
  116. MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
  117. subPropertyKey.type = MJPropertyKeyTypeArray;
  118. subPropertyKey.name = cmps[i];
  119. [propertyKeys addObject:subPropertyKey];
  120. }
  121. } else { // 没有索引的key
  122. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  123. propertyKey.name = oldKey;
  124. [propertyKeys addObject:propertyKey];
  125. }
  126. }
  127. return propertyKeys;
  128. }
  129. /** 对应着字典中的key */
  130. - (void)setOriginKey:(id)originKey forClass:(Class)c
  131. {
  132. if ([originKey isKindOfClass:[NSString class]]) { // 字符串类型的key
  133. NSArray *propertyKeys = [self propertyKeysWithStringKey:originKey];
  134. if (propertyKeys.count) {
  135. [self setPorpertyKeys:@[propertyKeys] forClass:c];
  136. }
  137. } else if ([originKey isKindOfClass:[NSArray class]]) {
  138. NSMutableArray *keyses = [NSMutableArray array];
  139. for (NSString *stringKey in originKey) {
  140. NSArray *propertyKeys = [self propertyKeysWithStringKey:stringKey];
  141. if (propertyKeys.count) {
  142. [keyses addObject:propertyKeys];
  143. }
  144. }
  145. if (keyses.count) {
  146. [self setPorpertyKeys:keyses forClass:c];
  147. }
  148. }
  149. }
  150. /** 对应着字典中的多级key */
  151. - (void)setPorpertyKeys:(NSArray *)propertyKeys forClass:(Class)c
  152. {
  153. if (propertyKeys.count == 0) return;
  154. NSString *key = NSStringFromClass(c);
  155. if (!key) return;
  156. MJ_LOCK(self.propertyKeysLock);
  157. self.propertyKeysDict[key] = propertyKeys;
  158. MJ_UNLOCK(self.propertyKeysLock);
  159. }
  160. - (NSArray *)propertyKeysForClass:(Class)c
  161. {
  162. NSString *key = NSStringFromClass(c);
  163. if (!key) return nil;
  164. MJ_LOCK(self.propertyKeysLock);
  165. NSArray *propertyKeys = self.propertyKeysDict[key];
  166. MJ_UNLOCK(self.propertyKeysLock);
  167. return propertyKeys;
  168. }
  169. /** 模型数组中的模型类型 */
  170. - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c
  171. {
  172. if (!objectClass) return;
  173. NSString *key = NSStringFromClass(c);
  174. if (!key) return;
  175. MJ_LOCK(self.objectClassInArrayLock);
  176. self.objectClassInArrayDict[key] = objectClass;
  177. MJ_UNLOCK(self.objectClassInArrayLock);
  178. }
  179. - (Class)objectClassInArrayForClass:(Class)c
  180. {
  181. NSString *key = NSStringFromClass(c);
  182. if (!key) return nil;
  183. MJ_LOCK(self.objectClassInArrayLock);
  184. Class objectClass = self.objectClassInArrayDict[key];
  185. MJ_UNLOCK(self.objectClassInArrayLock);
  186. return objectClass;
  187. }
  188. @end