-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseModel.m
159 lines (136 loc) · 6.2 KB
/
BaseModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// BaseModel.m
// WYCode
//
// Created by 王岩 on 14-12-22.
// Copyright (c) 2014年 wangyan. All rights reserved.
//
#import "BaseModel.h"
#import <objc/runtime.h>
@interface BaseModel ()
- (NSDictionary *)properties_aps;
@end
@implementation BaseModel
-(id) initWithJSON:(id)JSon{
self = [self init];
if (self) {
[self toEntity:JSon];
}
return self;
}
-(NSDictionary*)toDic
{
NSDictionary *aa = [self properties_aps];
return aa;
}
-(void) toEntity:(id)JSon
{
if (!JSon||![JSon isKindOfClass:[NSDictionary class]]) return;
NSDictionary *dic = JSon;
Class class = [self class];
while (![NSStringFromClass(class) isEqualToString:@"NSObject"]) {
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(class, &outCount);
// NSLog(@"outcount=%d",outCount);
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
//NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease];
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
// NSLog(@"propertyname=%@",propertyName);
NSString* type= [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
// NSLog(@"type=%@",type);
type=[type substringFromIndex:3];
NSRange range=[type rangeOfString:@","];
type=[type substringToIndex:range.location-1];
// NSLog(@"type=%@",type);
Class classs=NSClassFromString(type);
if ([classs isSubclassOfClass:[BaseModel class]]) {
id subModel=[[classs alloc]init];
[subModel toEntity:[dic objectForKey:propertyName]];
[self setValue:subModel forKey:propertyName];
}
else if ([classs isSubclassOfClass:[NSArray class]])
{
NSArray* arrayValue=[dic objectForKey:propertyName];
//[self setValue:arrayValue forKey:propertyName];
NSMutableArray* newArrayValue=[NSMutableArray arrayWithArray:arrayValue];
if([self respondsToSelector:@selector(objectClassInArray)])
{
NSDictionary* classDic=[self objectClassInArray];
//可以做到支持数组里存放不同的类,但是实际的项目中,没必要出现这种情况。
if (classDic&&classDic.count==1) {
Class clazz=classDic.allValues[0];
// NSLog(@"arryClass=%@",NSStringFromClass(clazz));
for (int j=0; j<newArrayValue.count; j++) {
id subArryModel=[[clazz alloc]init];
[subArryModel toEntity:arrayValue[j]];
// NSLog(@"subArrayModleClass=%@",NSStringFromClass([subArryModel class]));
newArrayValue[j]=subArryModel;
}
}
}
// NSLog(@"newArrayValue=%@",newArrayValue);
[ self setValue:newArrayValue forKey:propertyName];
}
else {
id virableStr = [dic objectForKey:propertyName];
//NSLog(@"值=%@",virableStr);
if (virableStr == [NSNull null]) {
NSLog(@"null啊啊啊 ");
// Do something for a null
//NSLog(@"haha=%@",virableStr);
}
// else if(virableStr!=nil&&virableStr!=@"") {
// [self setValue:virableStr forKey:propertyName];
// }
else if(virableStr&&virableStr!=nil) {
[self setValue:virableStr forKey:propertyName];
}
else
[self setValue:@"" forKeyPath:propertyName];
}
}
free(properties);
class = [class superclass];
// NSLog(@"superclass=%@",NSStringFromClass(class));
}
}
- (NSDictionary *)properties_aps {
NSMutableDictionary *props = [NSMutableDictionary dictionary];
Class class = [self class];
while (![NSStringFromClass(class) isEqualToString:@"NSObject"]) {
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(class, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
//NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease];
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
//NSString *attr = [[[NSString alloc] initWithCString:property_getAttributes(property)] autorelease];
id propertyValue = [self valueForKey:(NSString *)propertyName];
//NSLog(@"名称:%@--值:%@---属性:%@",propertyName,propertyValue,attr);
if ([propertyValue isKindOfClass:[BaseModel class]]) {
BaseModel *subClass = (BaseModel*)propertyValue;
propertyValue = [subClass toDic];
}
else if ([propertyValue isKindOfClass:[NSArray class]])
{
if([self respondsToSelector:@selector(objectClassInArray)])
{
NSMutableArray* newPropertyValue=[[NSMutableArray alloc]initWithCapacity:[propertyValue count]];
for (int j=0; j<[propertyValue count]; j++) {
id arrayModel=propertyValue[j];
NSDictionary* arryDic=[arrayModel toDic];
newPropertyValue[j]=arryDic;
}
propertyValue=newPropertyValue;
}
}
if (propertyValue) [props setObject:propertyValue forKey:propertyName];
}
free(properties);
class = [class superclass];
}
return props;
}
@end