MJProperty.m 5.1 KB

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