Skip to content

Commit 276adf8

Browse files
committed
Let replacedKey support @[@"key1", @"key2"]
Let replacedKey support @[@"key1", @"key2"]
1 parent 9706d28 commit 276adf8

File tree

9 files changed

+87
-52
lines changed

9 files changed

+87
-52
lines changed

MJExtension.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "MJExtension"
3-
s.version = "2.5.7"
3+
s.version = "2.5.8"
44
s.ios.deployment_target = '6.0'
55
s.osx.deployment_target = '10.8'
66
s.summary = "The fastest and most convenient conversion between JSON and model"

MJExtension/MJProperty.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828
/**** 同一个成员属性 - 父类和子类的行为可能不一致(originKey、propertyKeys、objectClassInArray) ****/
2929
/** 设置最原始的key */
30-
- (void)setOriginKey:(NSString *)originKey forClass:(Class)c;
31-
/** 对应着字典中的多级key(里面存放的都是MJPropertyKey对象*/
32-
- (NSArray *)propertyKeysFromClass:(Class)c;
30+
- (void)setOriginKey:(id)originKey forClass:(Class)c;
31+
/** 对应着字典中的多级key(里面存放的数组,数组里面都是MJPropertyKey对象*/
32+
- (NSArray *)propertyKeysForClass:(Class)c;
3333

3434
/** 模型数组中的模型类型 */
3535
- (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c;
36-
- (Class)objectClassInArrayFromClass:(Class)c;
36+
- (Class)objectClassInArrayForClass:(Class)c;
3737
/**** 同一个成员变量 - 父类和子类的行为可能不一致(key、keys、objectClassInArray) ****/
3838

3939
/**

MJExtension/MJProperty.m

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -88,50 +88,69 @@ - (void)setValue:(id)value forObject:(id)object
8888
[object setValue:value forKey:self.name];
8989
}
9090

91-
/** 对应着字典中的key */
92-
- (void)setOriginKey:(NSString *)originKey forClass:(Class)c
91+
/**
92+
* 通过字符串key创建对应的keys
93+
*/
94+
- (NSArray *)propertyKeysWithStringKey:(NSString *)stringKey
9395
{
94-
if (originKey.length == 0) return;
96+
if (stringKey.length == 0) return nil;
9597

96-
NSMutableArray *propertyKeys = originKeyPropertyKey_[originKey];
97-
if (propertyKeys == nil) {
98-
// 存放所有的key
99-
originKeyPropertyKey_[originKey] = propertyKeys = [NSMutableArray array];
100-
101-
// 如果有多级映射
102-
NSArray *oldKeys = [originKey componentsSeparatedByString:@"."];
103-
104-
for (NSString *oldKey in oldKeys) {
105-
NSUInteger start = [oldKey rangeOfString:@"["].location;
106-
if (start != NSNotFound) { // 有索引的key
107-
NSString *prefixKey = [oldKey substringToIndex:start];
108-
NSString *indexKey = prefixKey;
109-
if (prefixKey.length) {
110-
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
111-
propertyKey.name = prefixKey;
112-
[propertyKeys addObject:propertyKey];
113-
114-
indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
115-
}
116-
117-
/** 解析索引 **/
118-
// 元素
119-
NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
120-
for (NSInteger i = 0; i<cmps.count - 1; i++) {
121-
MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
122-
subPropertyKey.type = MJPropertyKeyTypeArray;
123-
subPropertyKey.name = cmps[i];
124-
[propertyKeys addObject:subPropertyKey];
125-
}
126-
} else { // 没有索引的key
98+
NSMutableArray *propertyKeys = [NSMutableArray array];
99+
// 如果有多级映射
100+
NSArray *oldKeys = [stringKey componentsSeparatedByString:@"."];
101+
102+
for (NSString *oldKey in oldKeys) {
103+
NSUInteger start = [oldKey rangeOfString:@"["].location;
104+
if (start != NSNotFound) { // 有索引的key
105+
NSString *prefixKey = [oldKey substringToIndex:start];
106+
NSString *indexKey = prefixKey;
107+
if (prefixKey.length) {
127108
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
128-
propertyKey.name = oldKey;
109+
propertyKey.name = prefixKey;
129110
[propertyKeys addObject:propertyKey];
111+
112+
indexKey = [oldKey stringByReplacingOccurrencesOfString:prefixKey withString:@""];
130113
}
114+
115+
/** 解析索引 **/
116+
// 元素
117+
NSArray *cmps = [[indexKey stringByReplacingOccurrencesOfString:@"[" withString:@""] componentsSeparatedByString:@"]"];
118+
for (NSInteger i = 0; i<cmps.count - 1; i++) {
119+
MJPropertyKey *subPropertyKey = [[MJPropertyKey alloc] init];
120+
subPropertyKey.type = MJPropertyKeyTypeArray;
121+
subPropertyKey.name = cmps[i];
122+
[propertyKeys addObject:subPropertyKey];
123+
}
124+
} else { // 没有索引的key
125+
MJPropertyKey *propertyKey = [[MJPropertyKey alloc] init];
126+
propertyKey.name = oldKey;
127+
[propertyKeys addObject:propertyKey];
131128
}
132129
}
133130

134-
[self setPorpertyKeys:propertyKeys forClass:c];
131+
return propertyKeys;
132+
}
133+
134+
/** 对应着字典中的key */
135+
- (void)setOriginKey:(id)originKey forClass:(Class)c
136+
{
137+
if ([originKey isKindOfClass:[NSString class]]) { // 字符串类型的key
138+
NSArray *propertyKeys = [self propertyKeysWithStringKey:originKey];
139+
if (propertyKeys.count) {
140+
[self setPorpertyKeys:@[propertyKeys] forClass:c];
141+
}
142+
} else if ([originKey isKindOfClass:[NSArray class]]) {
143+
NSMutableArray *keyses = [NSMutableArray array];
144+
for (NSString *stringKey in originKey) {
145+
NSArray *propertyKeys = [self propertyKeysWithStringKey:stringKey];
146+
if (propertyKeys.count) {
147+
[keyses addObject:propertyKeys];
148+
}
149+
}
150+
if (keyses.count) {
151+
[self setPorpertyKeys:keyses forClass:c];
152+
}
153+
}
135154
}
136155

137156
/** 对应着字典中的多级key */
@@ -140,7 +159,7 @@ - (void)setPorpertyKeys:(NSArray *)propertyKeys forClass:(Class)c
140159
if (propertyKeys.count == 0) return;
141160
self.propertyKeysDict[NSStringFromClass(c)] = propertyKeys;
142161
}
143-
- (NSArray *)propertyKeysFromClass:(Class)c
162+
- (NSArray *)propertyKeysForClass:(Class)c
144163
{
145164
return self.propertyKeysDict[NSStringFromClass(c)];
146165
}
@@ -151,7 +170,7 @@ - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c
151170
if (!objectClass) return;
152171
self.objectClassInArrayDict[NSStringFromClass(c)] = objectClass;
153172
}
154-
- (Class)objectClassInArrayFromClass:(Class)c
173+
- (Class)objectClassInArrayForClass:(Class)c
155174
{
156175
return self.objectClassInArrayDict[NSStringFromClass(c)];
157176
}

MJExtension/NSObject+MJKeyValue.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,16 @@
180180
+ (NSMutableArray *)objectArrayWithFile:(NSString *)file error:(NSError **)error;
181181

182182
#pragma mark - 转换为JSON
183+
/**
184+
* 转换为JSON Data
185+
*/
183186
- (NSData *)JSONData;
187+
/**
188+
* 转换为字典或者数组
189+
*/
184190
- (id)JSONObject;
191+
/**
192+
* 转换为JSON 字符串
193+
*/
185194
- (NSString *)JSONString;
186195
@end

MJExtension/NSObject+MJKeyValue.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
8686
if ([ignoredPropertyNames containsObject:property.name]) return;
8787

8888
// 1.取出属性值
89-
id value = keyValues ;
90-
NSArray *propertyKeys = [property propertyKeysFromClass:aClass];
91-
for (MJPropertyKey *propertyKey in propertyKeys) {
92-
value = [propertyKey valueInObject:value];
89+
id value;
90+
NSArray *propertyKeyses = [property propertyKeysForClass:aClass];
91+
for (NSArray *propertyKeys in propertyKeyses) {
92+
value = keyValues;
93+
for (MJPropertyKey *propertyKey in propertyKeys) {
94+
value = [propertyKey valueInObject:value];
95+
}
96+
if (value) break;
9397
}
9498

9599
// 值的过滤
@@ -102,7 +106,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
102106
// 2.如果是模型属性
103107
MJPropertyType *type = property.type;
104108
Class typeClass = type.typeClass;
105-
Class objectClass = [property objectClassInArrayFromClass:[self class]];
109+
Class objectClass = [property objectClassInArrayForClass:[self class]];
106110
if (!type.isFromFoundation && typeClass) {
107111
value = [typeClass objectWithKeyValues:value context:context error:error];
108112
} else if (objectClass) {
@@ -349,7 +353,7 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray
349353

350354
// 4.赋值
351355
if ([aClass isReferenceReplacedKeyWhenCreatingKeyValues]) {
352-
NSArray *propertyKeys = [property propertyKeysFromClass:aClass];
356+
NSArray *propertyKeys = [[property propertyKeysForClass:aClass] firstObject];
353357
NSUInteger keyCount = propertyKeys.count;
354358
// 创建字典
355359
__block id innerContainer = keyValues;

MJExtensionExample/MJExtensionExample/Classes/MJExtensionConfig.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ + (void)load
6060
@"desc" : @"desciption",
6161
@"oldName" : @"name.oldName",
6262
@"nowName" : @"name.newName",
63+
@"otherName" : @[@"otherName", @"name.newName", @"name.oldName"],
6364
@"nameChangedTime" : @"name.info[1].nameChangedTime",
6465
@"bag" : @"other.bag"
6566
};

MJExtensionExample/MJExtensionExample/Classes/Student.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@interface Student : NSObject
1313
@property (copy, nonatomic) NSString *ID;
14+
@property (copy, nonatomic) NSString *otherName;
1415
@property (copy, nonatomic) NSString *nowName;
1516
@property (copy, nonatomic) NSString *oldName;
1617
@property (copy, nonatomic) NSString *nameChangedTime;

MJExtensionExample/MJExtensionExample/main.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,16 @@ void keyValues2object4()
221221
@"name" : @"小书包",
222222
@"price" : @100.7
223223
}
224-
}
224+
},
225+
// @"otherName" : @"test"
225226
};
226227

227228
// 2.将字典转为Student模型
228229
Student *stu = [Student objectWithKeyValues:dict];
229230

230231
// 3.打印Student模型的属性
231-
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
232-
stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
232+
NSLog(@"ID=%@, desc=%@, otherName=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
233+
stu.ID, stu.desc, stu.otherName, stu.oldName, stu.nowName, stu.nameChangedTime);
233234
NSLog(@"bagName=%@, bagPrice=%f",
234235
stu.bag.name, stu.bag.price);
235236
}

0 commit comments

Comments
 (0)