MJProperty.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. @interface MJProperty()
  13. @property (strong, nonatomic) NSMutableDictionary *propertyKeysDict;
  14. @property (strong, nonatomic) NSMutableDictionary *objectClassInArrayDict;
  15. @end
  16. @implementation MJProperty
  17. #pragma mark - 懒加载
  18. - (NSMutableDictionary *)propertyKeysDict
  19. {
  20. if (!_propertyKeysDict) {
  21. _propertyKeysDict = [NSMutableDictionary dictionary];
  22. }
  23. return _propertyKeysDict;
  24. }
  25. - (NSMutableDictionary *)objectClassInArrayDict
  26. {
  27. if (!_objectClassInArrayDict) {
  28. _objectClassInArrayDict = [NSMutableDictionary dictionary];
  29. }
  30. return _objectClassInArrayDict;
  31. }
  32. #pragma mark - 缓存
  33. + (instancetype)cachedPropertyWithProperty:(objc_property_t)property
  34. {
  35. MJProperty *propertyObj = objc_getAssociatedObject(self, property);
  36. if (propertyObj == nil) {
  37. propertyObj = [[self alloc] init];
  38. propertyObj.property = property;
  39. objc_setAssociatedObject(self, property, propertyObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  40. }
  41. return propertyObj;
  42. }
  43. #pragma mark - 公共方法
  44. - (void)setProperty:(objc_property_t)property
  45. {
  46. _property = property;
  47. MJExtensionAssertParamNotNil(property);
  48. // 1.属性名
  49. _name = @(property_getName(property));
  50. // 2.成员类型
  51. NSString *attrs = @(property_getAttributes(property));
  52. NSUInteger dotLoc = [attrs rangeOfString:@","].location;
  53. NSString *code = nil;
  54. NSUInteger loc = 1;
  55. if (dotLoc == NSNotFound) { // 没有,
  56. code = [attrs substringFromIndex:loc];
  57. } else {
  58. code = [attrs substringWithRange:NSMakeRange(loc, dotLoc - loc)];
  59. }
  60. _type = [MJPropertyType cachedTypeWithCode:code];
  61. }
  62. /**
  63. * 获得成员变量的值
  64. */
  65. - (id)valueForObject:(id)object
  66. {
  67. if (self.type.KVCDisabled) return [NSNull null];
  68. return [object valueForKey:self.name];
  69. }
  70. /**
  71. * 设置成员变量的值
  72. */
  73. - (void)setValue:(id)value forObject:(id)object
  74. {
  75. if (self.type.KVCDisabled || value == nil) return;
  76. [object setValue:value forKey:self.name];
  77. }
  78. /**
  79. * 通过字符串key创建对应的keys
  80. */
  81. - (NSArray *)propertyKeysWithStringKey:(NSString *)stringKey
  82. {
  83. if (stringKey.length == 0) return nil;
  84. NSMutableArray *propertyKeys = [NSMutableArray array];
  85. // 如果有多级映射
  86. NSArray *oldKeys = [stringKey componentsSeparatedByString:@"."];
  87. for (NSString *oldKey in oldKeys) {
  88. NSUInteger start = [oldKey rangeOfString:@"["].location;
  89. if (start != NSNotFound) { // 有索引的key
  90. NSString *prefixKey = [oldKey substringToIndex:start];
  91. NSString *indexKey = prefixKey;
  92. if (prefixKey.length) {
  93. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  94. propertyKey.name = prefixKey;
  95. [propertyKeys addObject:propertyKey];
  96. indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
  97. }
  98. /** 解析索引 **/
  99. // 元素
  100. NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
  101. for (NSInteger i = 0; i<cmps.count - 1; i++) {
  102. MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
  103. subPropertyKey.type = MJPropertyKeyTypeArray;
  104. subPropertyKey.name = cmps[i];
  105. [propertyKeys addObject:subPropertyKey];
  106. }
  107. } else { // 没有索引的key
  108. MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
  109. propertyKey.name = oldKey;
  110. [propertyKeys addObject:propertyKey];
  111. }
  112. }
  113. return propertyKeys;
  114. }
  115. /** 对应着字典中的key */
  116. - (void)setOriginKey:(id)originKey forClass:(Class)c
  117. {
  118. if ([originKey isKindOfClass:[NSString class]]) { // 字符串类型的key
  119. NSArray *propertyKeys = [self propertyKeysWithStringKey:originKey];
  120. if (propertyKeys.count) {
  121. [self setPorpertyKeys:@[propertyKeys] forClass:c];
  122. }
  123. } else if ([originKey isKindOfClass:[NSArray class]]) {
  124. NSMutableArray *keyses = [NSMutableArray array];
  125. for (NSString *stringKey in originKey) {
  126. NSArray *propertyKeys = [self propertyKeysWithStringKey:stringKey];
  127. if (propertyKeys.count) {
  128. [keyses addObject:propertyKeys];
  129. }
  130. }
  131. if (keyses.count) {
  132. [self setPorpertyKeys:keyses forClass:c];
  133. }
  134. }
  135. }
  136. /** 对应着字典中的多级key */
  137. - (void)setPorpertyKeys:(NSArray *)propertyKeys forClass:(Class)c
  138. {
  139. if (propertyKeys.count == 0) return;
  140. self.propertyKeysDict[NSStringFromClass(c)] = propertyKeys;
  141. }
  142. - (NSArray *)propertyKeysForClass:(Class)c
  143. {
  144. return self.propertyKeysDict[NSStringFromClass(c)];
  145. }
  146. /** 模型数组中的模型类型 */
  147. - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c
  148. {
  149. if (!objectClass) return;
  150. self.objectClassInArrayDict[NSStringFromClass(c)] = objectClass;
  151. }
  152. - (Class)objectClassInArrayForClass:(Class)c
  153. {
  154. return self.objectClassInArrayDict[NSStringFromClass(c)];
  155. }
  156. @end