Skip to content

Commit

Permalink
[Model referenceReplacedKeyWhenCreatingKeyValues:YES];
Browse files Browse the repository at this point in the history
[Model referenceReplacedKeyWhenCreatingKeyValues:YES];
  • Loading branch information
CoderMJLee committed Jul 13, 2015
1 parent 093f5bc commit 1eb2923
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MJExtension.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MJExtension"
s.version = "2.4.3"
s.version = "2.5.0"
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.summary = "The fastest and most convenient conversion between JSON and model"
Expand Down
6 changes: 6 additions & 0 deletions MJExtension/NSObject+MJKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context;
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error;

/**
* 模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法
*/
+ (void)referenceReplacedKeyWhenCreatingKeyValues:(BOOL)reference;
+ (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues;

/**
* 将模型转成字典
* @return 字典
Expand Down
60 changes: 53 additions & 7 deletions MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

@implementation NSObject (MJKeyValue)

static const char MJIgnoreReplacedKeyWhenGettingKeyValuesKey = '\0';
static const char MJReferenceReplacedKeyWhenCreatingKeyValuesKey = '\0';

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

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

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

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

@try {
Class aClass = [self class];
Expand Down Expand Up @@ -336,7 +336,53 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray
}

// 4.赋值
keyValues[property.name] = value;
if ([aClass isReferenceReplacedKeyWhenCreatingKeyValues]) {
NSArray *propertyKeys = [property propertyKeysFromClass:aClass];
NSUInteger keyCount = propertyKeys.count;
// 创建字典
__block id innerContainer = keyValues;
[propertyKeys enumerateObjectsUsingBlock:^(MJPropertyKey *propertyKey, NSUInteger idx, BOOL *stop) {
// 下一个属性
MJPropertyKey *nextPropertyKey = nil;
if (idx != keyCount - 1) {
nextPropertyKey = propertyKeys[idx + 1];
}

if (nextPropertyKey) { // 不是最后一个key
// 当前propertyKey对应的字典或者数组
id tempInnerContainer = [propertyKey valueForObject:innerContainer];
if (tempInnerContainer == nil || [tempInnerContainer isKindOfClass:[NSNull class]]) {
if (nextPropertyKey.type == MJPropertyKeyTypeDictionary) {
tempInnerContainer = [NSMutableDictionary dictionary];
} else {
tempInnerContainer = [NSMutableArray array];
}
if (propertyKey.type == MJPropertyKeyTypeDictionary) {
innerContainer[propertyKey.name] = tempInnerContainer;
} else {
innerContainer[propertyKey.name.intValue] = tempInnerContainer;
}
}

if ([tempInnerContainer isKindOfClass:[NSMutableArray class]]) {
int index = nextPropertyKey.name.intValue;
while ([tempInnerContainer count] < index + 1) {
[tempInnerContainer addObject:[NSNull null]];
}
}

innerContainer = tempInnerContainer;
} else { // 最后一个key
if (propertyKey.type == MJPropertyKeyTypeDictionary) {
innerContainer[propertyKey.name] = value;
} else {
innerContainer[propertyKey.name.intValue] = value;
}
}
}];
} else {
keyValues[property.name] = value;
}
}];

// 去除系统自动增加的元素
Expand Down
3 changes: 3 additions & 0 deletions MJExtensionExample/MJExtensionExample/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ void object2keyValues()
NSLog(@"%@", stuDict);
NSLog(@"%@", [stu keyValuesWithIgnoredKeys:@[@"bag", @"oldName", @"nowName"]]);
NSLog(@"%@", stu.JSONString);

[Student referenceReplacedKeyWhenCreatingKeyValues:YES];
NSLog(@"\n模型转字典时,字典的key是否参考replacedKeyFromPropertyName等方法:\n%@", stu.keyValues);
}

/**
Expand Down

0 comments on commit 1eb2923

Please sign in to comment.