Skip to content

Commit 1e4d584

Browse files
committed
模型属性自定义类型转换
1 parent 81e9014 commit 1e4d584

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

MCJSONKit/Classes/MCJSONKitProperty.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
封装类的属性
1818
*/
19-
@interface JSONCoreProperty : NSObject
19+
@interface MCJSONKitProperty : NSObject
2020

2121
/**
2222
属性名称

MCJSONKit/Classes/MCJSONKitProperty.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
#import "MCJSONKitProperty.h"
1010

11-
@implementation JSONCoreProperty
11+
@implementation MCJSONKitProperty
1212

1313
@end

MCJSONKit/Classes/NSObject+MCJSONKit.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//
22
// NSObject+JSONCore.h
3-
// iOSExample
3+
// MCJSONKit
44
//
55
// Created by maintoco on 16/12/21.
66
// Copyright © 2016年 maintoco. All rights reserved.
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import "MCJSONKitProperty.h"
1011

1112
/**
1213
JSON解析类别
@@ -66,4 +67,13 @@
6667
*/
6768
- (NSDictionary *)mc_typeMappingDictionary;
6869

70+
/**
71+
自定义类型的值转换
72+
73+
@param oldValue 旧值
74+
@param property 属性
75+
@return 新值
76+
*/
77+
- (id)mc_newValueFromOldValue:(id)oldValue property:(id)property;
78+
6979
@end

MCJSONKit/Classes/NSObject+MCJSONKit.m

+13-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88

99
#import "NSObject+MCJSONKit.h"
10-
#import "MCJSONKitProperty.h"
1110
#import "NSDictionary+MCJSONKit.h"
1211

1312
#import <objc/runtime.h>
@@ -119,8 +118,8 @@ - (void)setValuesWithJSONString:(NSString *)jsonString {
119118

120119
- (void)setValuesWithDictionary:(NSDictionary *)dict {
121120
if (dict && [dict isKindOfClass:[NSDictionary class]]) {
122-
NSMutableDictionary<NSString *,JSONCoreProperty *> *mdict = [self allProperties];
123-
[mdict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, JSONCoreProperty * _Nonnull obj, BOOL * _Nonnull stop) {
121+
NSMutableDictionary<NSString *,MCJSONKitProperty *> *mdict = [self allProperties];
122+
[mdict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, MCJSONKitProperty * _Nonnull obj, BOOL * _Nonnull stop) {
124123
if ([obj.jsonKey rangeOfString:@"."].location == NSNotFound) {
125124
[self setValue:dict[obj.jsonKey] forProperty:obj];
126125
}else {
@@ -137,7 +136,7 @@ - (void)setValuesWithDictionary:(NSDictionary *)dict {
137136
/**
138137
设置属性值
139138
*/
140-
- (void)setValue:(id)value forProperty:(JSONCoreProperty *)property {
139+
- (void)setValue:(id)value forProperty:(MCJSONKitProperty *)property {
141140
if ([value isKindOfClass:[NSNull class]]) {
142141
//NSNull不做处理
143142
return;
@@ -160,7 +159,8 @@ - (void)setValue:(id)value forProperty:(JSONCoreProperty *)property {
160159
}
161160
[self setValue:newValue forKey:property.name];
162161
}
163-
}else {
162+
} else {
163+
value = [self mc_newValueFromOldValue:value property:property];
164164
if ([value isKindOfClass:[NSNumber class]]) {
165165
if ([property.typeClass isSubclassOfClass:[NSString class]]) {
166166
//属性是NSString,但是值类型为NSNumber
@@ -197,7 +197,7 @@ - (void)setValue:(id)value forProperty:(JSONCoreProperty *)property {
197197
}
198198
}
199199

200-
- (id)valueForProperty:(JSONCoreProperty *)property {
200+
- (id)valueForProperty:(MCJSONKitProperty *)property {
201201
if (property) {
202202
id value = [self valueForKey:property.name];
203203
if (property.typeClass) {
@@ -283,7 +283,7 @@ - (NSMutableDictionary *)allProperties {
283283
}
284284

285285
//类型说明
286-
JSONCoreProperty *coprop = [JSONCoreProperty new];
286+
MCJSONKitProperty *coprop = [MCJSONKitProperty new];
287287
coprop.name = propertyName;
288288

289289
//自定义映射
@@ -347,9 +347,9 @@ - (NSDictionary *)mc_toDictionary {
347347
return jsonObj;
348348
}
349349

350-
NSDictionary<NSString *,JSONCoreProperty *> *dict = [self allProperties];
350+
NSDictionary<NSString *,MCJSONKitProperty *> *dict = [self allProperties];
351351
NSMutableDictionary *mdict = [NSMutableDictionary dictionary];
352-
[dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, JSONCoreProperty * _Nonnull obj, BOOL * _Nonnull stop) {
352+
[dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, MCJSONKitProperty * _Nonnull obj, BOOL * _Nonnull stop) {
353353
void(^setValueBlock)(id) = ^(id value) {
354354
if (value) {
355355
[mdict setValue:value forKeyPath:obj.jsonKey];
@@ -414,4 +414,8 @@ - (NSDictionary *)mc_typeMappingDictionary {
414414
return nil;
415415
}
416416

417+
- (id)mc_newValueFromOldValue:(id)oldValue property:(MCJSONKitProperty *)property {
418+
return oldValue;
419+
}
420+
417421
@end

0 commit comments

Comments
 (0)