Skip to content

Commit

Permalink
加入了对数组的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
wcxdell authored and wcxdell committed Jun 21, 2016
1 parent ebfcf4c commit 9c2a4d5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 6 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions CXModelExample/CXProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typedef NS_ENUM(NSUInteger,CXPropertyType){
CXPropertyTypeNumber = 0,
CXPropertyTypeSystem,
CXPropertyTypeDate,
CXPropertyTypeArray,
CXPropertyTypeCustom
};

Expand Down
2 changes: 2 additions & 0 deletions CXModelExample/CXProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ + (instancetype) initWithProperty:(objc_property_t) property{
NSString *tmp = [typeString substringWithRange:NSMakeRange(2, typeString.length - 3)];
if ([tmp isEqualToString:@"NSDate"]) {
cxProperty.type = CXPropertyTypeDate;
}else if([tmp isEqualToString:@"NSArray"]){
cxProperty.type = CXPropertyTypeArray;
}else if ([systemClass containsObject:NSClassFromString(tmp)]){
cxProperty.type = CXPropertyTypeSystem;
}else{
Expand Down
1 change: 1 addition & 0 deletions CXModelExample/NSObject+CXModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

@interface NSObject (CXModel)
+ (instancetype) objectWithJSON:(id) json;
+ (NSArray*) arrayWithJSON:(id) json;
@end
22 changes: 19 additions & 3 deletions CXModelExample/NSObject+CXModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ + (instancetype) objectWithJSON:(id) json{
return [object setProperties:json];
}

+ (NSArray *)arrayWithJSON:(id)json{
json = [json getDic];
if (!json || json == [NSNull null]) return nil;

NSMutableArray *array = [NSMutableArray array];

for (NSDictionary *dic in json) {
if ([dic isKindOfClass:[NSArray class]]) {
[array addObject:[self arrayWithJSON:dic]];
}else{
[array addObject:[self objectWithJSON:dic]];
}
}
return array;
}

- (instancetype) setProperties:(NSDictionary*)dic{

//获取属性列表
Expand All @@ -35,10 +51,10 @@ - (instancetype) setProperties:(NSDictionary*)dic{

if (property.type == CXPropertyTypeDate) {
value = CXNSDateFromString(value);
}

if (property.type == CXPropertyTypeCustom) {
}else if (property.type == CXPropertyTypeCustom) {
value = [property.typeClass objectWithJSON:value];
}else if (property.type == CXPropertyTypeArray){
//TODO:字典中的数组需要用户自己指定类型,用协议实现
}

if (!value || value == [NSNull null]) {
Expand Down
1 change: 1 addition & 0 deletions CXModelExample/Student.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
@property (nonatomic, assign) double doubleProperty;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) Student *smallStudent;
@property (nonatomic, strong) NSArray *studentArray;
@end
34 changes: 31 additions & 3 deletions CXModelExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,40 @@ - (void)viewDidLoad {
@"smallStudent":@{
@"name":@"changxu",
@"age":@24
}
},
@"studentArray":@[
@{
@"name":@"changxu",
@"studentId":@"2015"
},
@{
@"age":@14,
@"date":@"1992-02-18"
}
]
};
Student *student = [Student objectWithJSON:dic];

NSLog(@"%g",student.doubleProperty);

Student *student = [Student objectWithJSON:dic];


NSLog(@"%g",student.doubleProperty);

// NSArray *array = @[
// @{
// @"name":@"changxu",
// @"studentId":@"2015"
// },
// @{
// @"age":@14,
// @"date":@"1992-02-18"
// }
// ];
//
//
//
// NSArray *studentArray = [Student arrayWithJSON:array];
// NSLog(@"%@",studentArray);
}

- (void)didReceiveMemoryWarning {
Expand Down

0 comments on commit 9c2a4d5

Please sign in to comment.