Skip to content

Commit

Permalink
Abandon old model -> keyValues way
Browse files Browse the repository at this point in the history
Abandon old model -> keyValues way
  • Loading branch information
CoderMJLee committed Jul 9, 2015
1 parent 33bab1c commit 95133f7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 114 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.2"
s.version = "2.4.3"
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
14 changes: 6 additions & 8 deletions MJExtension/NSObject+MJKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context;
- (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)context error:(NSError **)error;

/** 是否在模型转字典时,忽略替换过的key */
@property (nonatomic, assign, getter=isIgnoreReplacedKeyWhenGettingKeyValues) BOOL ignoreReplacedKeyWhenGettingKeyValues;
/**
* 将模型转成字典
* @return 字典
*/
- (id)keyValues;
- (id)keyValuesWithKeys:(NSArray *)keys;
- (id)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys;
- (id)keyValuesWithError:(NSError **)error;
- (id)keyValuesWithKeys:(NSArray *)keys error:(NSError **)error;
- (id)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys error:(NSError **)error;
- (NSMutableDictionary *)keyValues;
- (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys;
- (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys;
- (NSMutableDictionary *)keyValuesWithError:(NSError **)error;
- (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys error:(NSError **)error;
- (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys error:(NSError **)error;

/**
* 通过模型数组来创建一个字典数组
Expand Down
91 changes: 9 additions & 82 deletions MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,46 +270,42 @@ + (NSMutableArray *)objectArrayWithFile:(NSString *)file error:(NSError *__autor
}

#pragma mark - 模型 -> 字典
- (id)keyValues
- (NSMutableDictionary *)keyValues
{
return [self keyValuesWithError:nil];
}

- (id)keyValuesWithError:(NSError *__autoreleasing *)error
- (NSMutableDictionary *)keyValuesWithError:(NSError *__autoreleasing *)error
{
return [self keyValuesWithIgnoredKeys:nil error:error];
}

- (id)keyValuesWithKeys:(NSArray *)keys
- (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys
{
return [self keyValuesWithKeys:keys error:nil];
}

- (id)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys
- (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys
{
return [self keyValuesWithIgnoredKeys:ignoredKeys error:nil];
}

- (id)keyValuesWithKeys:(NSArray *)keys error:(NSError *__autoreleasing *)error
- (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys error:(NSError *__autoreleasing *)error
{
return [self keyValuesWithKeys:keys ignoredKeys:nil error:error];
}

- (id)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys error:(NSError *__autoreleasing *)error
- (NSMutableDictionary *)keyValuesWithIgnoredKeys:(NSArray *)ignoredKeys error:(NSError *__autoreleasing *)error
{
return [self keyValuesWithKeys:nil ignoredKeys:ignoredKeys error:error];
}

- (id)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray *)ignoredKeys error:(NSError *__autoreleasing *)error
- (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray *)ignoredKeys error:(NSError *__autoreleasing *)error
{
// 如果自己不是模型类
if ([MJFoundation isClassFromFoundation:[self class]]) return self;

__block id keyValues = nil;

if (self.isIgnoreReplacedKeyWhenGettingKeyValues) {
keyValues = [NSMutableDictionary dictionary];
}
id keyValues = [NSMutableDictionary dictionary];

@try {
Class aClass = [self class];
Expand Down Expand Up @@ -340,78 +336,9 @@ - (id)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray *)ignoredKeys error
}

// 4.赋值
if (self.isIgnoreReplacedKeyWhenGettingKeyValues) {
keyValues[property.name] = value;
} else {
NSArray *propertyKeys = [property propertyKeysFromClass:[self class]];
NSUInteger keyCount = propertyKeys.count;
// 创建字典
__block id innerContainer = nil;
[propertyKeys enumerateObjectsUsingBlock:^(MJPropertyKey *propertyKey, NSUInteger idx, BOOL *stop) {
// 创建keyValues
if (!keyValues) {
if (propertyKey.type == MJPropertyKeyTypeDictionary) {
keyValues = [NSMutableDictionary dictionary];
} else {
keyValues = [NSMutableArray array];
}
}
// 创建innerContainer
if (!innerContainer) {
innerContainer = keyValues;
if ([innerContainer isKindOfClass:[NSMutableArray class]]) {
int index = propertyKey.name.intValue;
while ([innerContainer count] < index + 1) {
[innerContainer addObject:[NSNull null]];
}
}
}

// 下一个属性
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;
}
}
}];
}
keyValues[property.name] = value;
}];

if (!keyValues) {
keyValues = [NSMutableDictionary dictionary];
}

// 去除系统自动增加的元素
if ([keyValues isKindOfClass:[NSMutableDictionary class]]) {
[keyValues removeObjectsForKeys:@[@"superclass", @"debugDescription", @"description", @"hash"]];
Expand Down
4 changes: 0 additions & 4 deletions MJExtensionExample/MJExtensionExample/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,6 @@ void object2keyValues()
NSDictionary *stuDict = stu.keyValues;
NSLog(@"%@", stuDict);
NSLog(@"%@", [stu keyValuesWithIgnoredKeys:@[@"bag", @"oldName", @"nowName"]]);

// 忽略配置的replacedKey,生成字典
stu.ignoreReplacedKeyWhenGettingKeyValues = YES;
NSLog(@"%@", stu.keyValues);
}

/**
Expand Down
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,17 @@ stu.bag = bag;
NSDictionary *stuDict = stu.keyValues;
NSLog(@"%@", stuDict);
/*
{
desciption = handsome;
id = 123;
name = {
info = (
"<null>"
,{
nameChangedTime = "2018-09-08";
});
newName = jack;
oldName = rose;
};
other = {
bag = {
name = "a red bag";
price = 205;
};
};
}
{
ID = 123;
bag = {
name = "\U5c0f\U4e66\U5305";
price = 205;
};
desc = handsome;
nameChangedTime = "2018-09-08";
nowName = jack;
oldName = rose;
}
*/
```

Expand Down

0 comments on commit 95133f7

Please sign in to comment.