Skip to content

Commit 1eb2923

Browse files
committed
[Model referenceReplacedKeyWhenCreatingKeyValues:YES];
[Model referenceReplacedKeyWhenCreatingKeyValues:YES];
1 parent 093f5bc commit 1eb2923

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
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.4.3"
3+
s.version = "2.5.0"
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/NSObject+MJKeyValue.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context;
8484
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error;
8585

86+
/**
87+
* 模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法
88+
*/
89+
+ (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference;
90+
+ (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues;
91+
8692
/**
8793
* 将模型转成字典
8894
* @return 字典

MJExtension/NSObject+MJKeyValue.m

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515

1616
@implementation NSObject (MJKeyValue)
1717

18-
static const char MJIgnoreReplacedKeyWhenGettingKeyValuesKey = '\0';
18+
static const char MJReferenceReplacedKeyWhenCreatingKeyValuesKey = '\0';
1919

20-
- (void)setIgnoreReplacedKeyWhenGettingKeyValues:(BOOL)ignoreReplacedKeyWhenGettingKeyValues
20+
+ (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference
2121
{
22-
objc_setAssociatedObject(self, &MJIgnoreReplacedKeyWhenGettingKeyValuesKey, @(ignoreReplacedKeyWhenGettingKeyValues), OBJC_ASSOCIATION_ASSIGN);
22+
objc_setAssociatedObject(self, &MJReferenceReplacedKeyWhenCreatingKeyValuesKey, @(reference), OBJC_ASSOCIATION_ASSIGN);
2323
}
2424

25-
- (BOOL)isIgnoreReplacedKeyWhenGettingKeyValues
25+
+ (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues
2626
{
27-
return [objc_getAssociatedObject(self, &MJIgnoreReplacedKeyWhenGettingKeyValuesKey) boolValue];
27+
return [objc_getAssociatedObject(self, &MJReferenceReplacedKeyWhenCreatingKeyValuesKey) boolValue];
2828
}
2929

3030
#pragma mark - --常用的对象--
@@ -111,7 +111,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
111111
keyValues = [NSJSONSerialization JSONObjectWithData:keyValues options:kNilOptions error:nil];
112112
}
113113

114-
MJAssertError([keyValues isKindOfClass:[NSDictionary class]] || [keyValues isKindOfClass:[NSArray class]], self, error, @"keyValues参数不是一个字典或者数组");
114+
MJAssertError([keyValues isKindOfClass:[NSDictionary class]], self, error, @"keyValues参数不是一个字典");
115115

116116
@try {
117117
Class aClass = [self class];
@@ -336,7 +336,53 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray
336336
}
337337

338338
// 4.赋值
339-
keyValues[property.name] = value;
339+
if ([aClass isReferenceReplacedKeyWhenCreatingKeyValues]) {
340+
NSArray *propertyKeys = [property propertyKeysFromClass:aClass];
341+
NSUInteger keyCount = propertyKeys.count;
342+
// 创建字典
343+
__block id innerContainer = keyValues;
344+
[propertyKeys enumerateObjectsUsingBlock:^(MJPropertyKey *propertyKey, NSUInteger idx, BOOL *stop) {
345+
// 下一个属性
346+
MJPropertyKey *nextPropertyKey = nil;
347+
if (idx != keyCount - 1) {
348+
nextPropertyKey = propertyKeys[idx + 1];
349+
}
350+
351+
if (nextPropertyKey) { // 不是最后一个key
352+
// 当前propertyKey对应的字典或者数组
353+
id tempInnerContainer = [propertyKey valueForObject:innerContainer];
354+
if (tempInnerContainer == nil || [tempInnerContainer isKindOfClass:[NSNull class]]) {
355+
if (nextPropertyKey.type == MJPropertyKeyTypeDictionary) {
356+
tempInnerContainer = [NSMutableDictionary dictionary];
357+
} else {
358+
tempInnerContainer = [NSMutableArray array];
359+
}
360+
if (propertyKey.type == MJPropertyKeyTypeDictionary) {
361+
innerContainer[propertyKey.name] = tempInnerContainer;
362+
} else {
363+
innerContainer[propertyKey.name.intValue] = tempInnerContainer;
364+
}
365+
}
366+
367+
if ([tempInnerContainer isKindOfClass:[NSMutableArray class]]) {
368+
int index = nextPropertyKey.name.intValue;
369+
while ([tempInnerContainer count] < index + 1) {
370+
[tempInnerContainer addObject:[NSNull null]];
371+
}
372+
}
373+
374+
innerContainer = tempInnerContainer;
375+
} else { // 最后一个key
376+
if (propertyKey.type == MJPropertyKeyTypeDictionary) {
377+
innerContainer[propertyKey.name] = value;
378+
} else {
379+
innerContainer[propertyKey.name.intValue] = value;
380+
}
381+
}
382+
}];
383+
} else {
384+
keyValues[property.name] = value;
385+
}
340386
}];
341387

342388
// 去除系统自动增加的元素

MJExtensionExample/MJExtensionExample/main.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ void object2keyValues()
298298
NSLog(@"%@", stuDict);
299299
NSLog(@"%@", [stu keyValuesWithIgnoredKeys:@[@"bag", @"oldName", @"nowName"]]);
300300
NSLog(@"%@", stu.JSONString);
301+
302+
[Student referenceReplacedKeyWhenCreatingKeyValues:YES];
303+
NSLog(@"\n模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法:\n%@", stu.keyValues);
301304
}
302305

303306
/**

0 commit comments

Comments
 (0)