forked from wuyuedefeng/NSDictionary-Model-Interconversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSTransObj.m
394 lines (362 loc) · 13.4 KB
/
WSTransObj.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// WSTransObj.m
// test_predicate
//
// Created by wangsen on 14-7-15.
// Copyright (c) 2014年 wangsen. All rights reserved.
//
#import "WSTransObj.h"
#import "NSDate+WSCategory.h"
#import "NSString+WSCategory.h"
static NSMutableDictionary *modalKeyGlobal;
//用于 创建的数据模型属性的Get和Set方法 Get方法在NSPredicate中用到 set方法暂未用到 不知道以后可用到什么场景 暂留
//get方法
/**
* 动态创建属性的get方法 Get方法在NSPredicate中用到
*
* @param self 动态创建的 模型对象
* @param _cmd 对象的属性
*
* @return get模型属性的值
*/
id nameGetter(id self, SEL _cmd) {
//Class wsclass = [self class];
NSString *name = [NSString stringWithFormat:@"%@",(NSStringFromSelector(_cmd))];
//Ivar ivar = class_getInstanceVariable([wsclass class], [name UTF8String]);
//NSLog(@"==%@",[WSTransObj valueGetterOfModal:self withKey:name]);
return [WSTransObj valueGetterOfModal:self withKey:name];
}
//set方法
/**
* 动态创建属性的set方法 方法暂未用到 不知道以后可用到什么场景 暂留
*
* @param self 动态创建的 模型对象
* @param _cmd 对象的属性
* @param newName 对象的属性 - 新值
*/
void nameSetter(id self, SEL _cmd, NSString *newName) {
NSString *name = [NSString stringWithFormat:@"%@",self];
[WSTransObj valueSetterOfModal:self withKey:name withValue:newName];
}
@implementation WSTransObj
#pragma mark - 获取模型的值
+ (id)valueGetterOfModal:(id)modal withKey:(NSString *)key
{
key = [@"_" stringByAppendingString:key];
Ivar ivar = class_getInstanceVariable([modal class], [key UTF8String]);
id value = object_getIvar(modal, ivar);
return value;
}
#pragma mark - 设置模型的值
+ (void)valueSetterOfModal:(id)modal withKey:(NSString *)key withValue:(id)value
{
key = [@"_" stringByAppendingString:key];
Ivar ivar = class_getInstanceVariable([modal class], [key UTF8String]);
id oldValue = object_getIvar(modal, ivar);
if (oldValue != value)
{
object_setIvar(modal, ivar, value);
}
}
#pragma mark - 字典数组转模型数组
+ (NSArray *)modalArray_from_dictionaryArr:(NSArray *)dictionaryArr
{
NSString *className = [[[[NSDate ws_current_DateTime] ws_md5_encrypt] stringByAppendingString:[NSString stringWithFormat:@"%d",arc4random()]] stringByAppendingString:@"_WSCLASS"];
return [WSTransObj modalArray_from_dictionaryArr:dictionaryArr token:className];
}
+ (NSArray *)modalArray_from_dictionaryArr:(NSArray *)dictionaryArr token:(NSString*)token
{
if (![dictionaryArr isKindOfClass:[NSArray class]]) {
NSLog(@"传递参数不是数组");
return nil;
}
if (dictionaryArr.count == 0) {
return nil;
}
if (![dictionaryArr[0] isKindOfClass:[NSDictionary class]]) {
return nil;
}
//保存创建模型的结构数据
if (!modalKeyGlobal) {
modalKeyGlobal = [NSMutableDictionary dictionary];
}
[modalKeyGlobal setObject:dictionaryArr[0] forKey:token];
Class wsclass = [WSTransObj createWSObjectClassWithOneDic:dictionaryArr[0] token:token];
return [WSTransObj modalArrWithDictionarys:dictionaryArr andModalClass:wsclass];
}
+ (id)modal_from_dictionary:(NSDictionary *)dic
{
if (dic) {
NSArray *arrDic = [NSArray arrayWithObjects:dic, nil];
return [WSTransObj modalArray_from_dictionaryArr:arrDic][0];
}
return nil;
}
+ (id)modal_from_dictionary:(NSDictionary *)dic token:(NSString*)token
{
if (dic) {
NSArray *arrDic = [NSArray arrayWithObjects:dic, nil];
return [WSTransObj modalArray_from_dictionaryArr:arrDic token:token][0];
}
return nil;
}
#pragma mark -私有调用方法
#pragma mark -create class WSObject
+ (Class)createWSObjectClassWithOneDic:(NSDictionary *)oneDic
{
//[WSTransObj createClass];
//创建WSTransModol类
//const char * className = "WSTransModal";
NSString *className = [[[[NSDate ws_current_DateTime] ws_md5_encrypt] stringByAppendingString:[NSString stringWithFormat:@"%d",arc4random()]] stringByAppendingString:@"_WSCLASS"];
return [WSTransObj createWSObjectClassWithOneDic:oneDic token:className];
}
+ (Class)createWSObjectClassWithOneDic:(NSDictionary *)oneDic token:(NSString *)token
{
const char * className = [token UTF8String];
Class wsclass = objc_getClass(className);
if (!wsclass)
{
Class superClass = [WSTransObj class];
wsclass = objc_allocateClassPair(superClass, className, 0);
NSArray *allKeys = [oneDic allKeys];
NSArray *allValues = [oneDic allValues];
NSUInteger count = allKeys.count;
for (int i = 0; i < count; i++) {
NSString *classType = NSStringFromClass([allValues[i] class]);
wsclass = [WSTransObj setClassIvar:classType andIvarName:[@"_" stringByAppendingString:allKeys[i]] andIvarValue:allValues[i] withClass:wsclass];
}
//4注册到运行时环境
objc_registerClassPair(wsclass);
}
return wsclass;
}
#pragma mark -对类设置属性
+ (Class)setClassIvar:(NSString *)ivarType andIvarName:(NSString *)ivarName andIvarValue:(NSString *)iVarValue withClass:(Class)wsclass
{
Class ivarTypeClass = NSClassFromString(ivarType);
class_addIvar(wsclass, [ivarName UTF8String], sizeof([ivarTypeClass class]), 0, [ivarType UTF8String]);
class_addMethod([wsclass class], NSSelectorFromString(ivarName), (IMP)nameGetter, "@@:");
NSString *setName = [NSString stringWithFormat:@"set%@:",[ivarName capitalizedString]];
class_addMethod([wsclass class], NSSelectorFromString(setName), (IMP)nameSetter, "v@:@");
return wsclass;
}
#pragma mark -字典数组转换成模型数组的具体实现
+ (NSArray *)modalArrWithDictionarys:(NSArray *)dicArr andModalClass:(Class)wsclass
{
NSMutableArray *mutableArr = [NSMutableArray array];
NSArray *allKeys = [dicArr[0] allKeys];
NSUInteger countArr = [dicArr count];
NSUInteger countKey = [allKeys count];
for (int i = 0; i<countArr; i++) {
NSArray *allValues = [dicArr[i] allValues];
id instance = [[wsclass alloc] init];
for (int j = 0; j<countKey; j++) {
//[instance setValue:allValues[j] forKey:allKeys[j]];
//Ivar ivar = class_getInstanceVariable([wsclass class], [allKeys[j] UTF8String]);
//object_setIvar(instance, ivar, allValues[j]);
NSLog(@"%@",allKeys[j]);
if ([allValues[j] isKindOfClass:[NSDictionary class]]) {
NSString *subToken = [NSStringFromClass(wsclass) stringByAppendingString:allKeys[j]];
id subModal;
Class wssubclass = [WSTransObj createWSObjectClassWithOneDic:allValues[j] token:subToken];
subModal = [WSTransObj modalArrWithDictionarys:@[allValues[j]] andModalClass:wssubclass][0];
[WSTransObj valueSetterOfModal:instance withKey:allKeys[j] withValue:subModal];
}
else
{
[WSTransObj valueSetterOfModal:instance withKey:allKeys[j] withValue:allValues[j]];
}
// [WSTransObj valueSetterOfModal:instance withKey:allKeys[j] withValue:allValues[j]];
}
NSLog(@"%@",instance);
[mutableArr addObject:instance];
}
return mutableArr;
}
#pragma mark - 模型数组转字典数组
+ (NSArray *)dictionaryArray_from_modalArray:(NSArray *)modalArray
{
if (![modalArray isKindOfClass:[NSArray class]]) {
NSLog(@"传递参数不是数组");
}
if (modalArray.count == 0) {
return nil;
}
//获取创建字典所需要的key key获取到都是字符串类型(NSString)
NSArray *allKeys = [WSTransObj typeNamesOfModal:modalArray[0]];
NSMutableArray *customAllKeys = [NSMutableArray array];
for (NSString *key in allKeys) {
[customAllKeys addObject:[key substringFromIndex:1]];
}
NSMutableArray *mutableArr = [NSMutableArray array];//待返回数组
for (id modal in modalArray) {
NSArray *allValues = [WSTransObj valuesOfModal:modal andModalKeys:customAllKeys];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:allValues forKeys:customAllKeys];
[mutableArr addObject:dic];
}
return mutableArr;
}
+ (id)dictionary_from_modal:(id)modol
{
if (modol) {
NSArray *modalArr = [NSArray arrayWithObjects:modol, nil];
return [WSTransObj dictionaryArray_from_modalArray:modalArr][0];
}
return nil;
}
#pragma mark -获取模型所有属性名称
+ (NSArray *)typeNamesOfModal:(id)modal
{
NSMutableArray *mutableArr = [NSMutableArray array];
unsigned int count = 0;
// 获得modal类中的所有成员变量
//NSLog(@"==%@",NSStringFromClass([modal class]));
Ivar *ivars = class_copyIvarList([modal class], &count);
// 遍历所有的成员变量
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
//NSLog(@"=%@",[NSString stringWithFormat:@"%s",ivar_getName(ivar)]);
[mutableArr addObject:[NSString stringWithFormat:@"%s",ivar_getName(ivar)]];
}
free(ivars);
BOOL hasSuperIvar = YES;
Class superClass = [modal class];
while (hasSuperIvar) {
superClass = [superClass superclass];
if ([superClass class] == [NSObject class]) {
hasSuperIvar = NO;
break;
}
Ivar *ivars = class_copyIvarList([superClass class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
//NSLog(@"=%@",[NSString stringWithFormat:@"%s",ivar_getName(ivar)]);
[mutableArr addObject:[NSString stringWithFormat:@"%s",ivar_getName(ivar)]];
}
free(ivars);
}
return mutableArr;
}
#pragma mark -获取模型所有属性的值
+ (NSArray *)valuesOfModal:(id)modal andModalKeys:(NSArray *)allKeys
{
NSMutableArray *mutableArr = [NSMutableArray array];
for (NSString *key in allKeys) {
id obj = [WSTransObj valueGetterOfModal:modal withKey:key];
if ([WSTransObj isBaseObject:obj]) {
[mutableArr addObject:[WSTransObj valueGetterOfModal:modal withKey:key]];
}
else
{
if (!obj) {
[mutableArr addObject:[NSNull null]];
}
else
{
[mutableArr addObject:[WSTransObj dictionary_from_modal:obj]];
}
}
// [mutableArr addObject:[WSTransObj valueGetterOfModal:modal withKey:key]];
}
return mutableArr;
}
+ (BOOL)isBaseObject:(id)obj
{
if ([obj isKindOfClass:[NSDictionary class]]) {
return YES;
}
if ([obj isKindOfClass:[NSArray class]]) {
return YES;
}
if ([obj isKindOfClass:[NSString class]]) {
return YES;
}
if ([obj isKindOfClass:[NSNumber class]]) {
return YES;
}
if ([obj isKindOfClass:[NSData class]]) {
return YES;
}
if ([obj isKindOfClass:[NSDate class]]) {
return YES;
}
if ([obj isKindOfClass:[NSSet class]]) {
return YES;
}
if ([obj isKindOfClass:[NSSet class]]) {
return YES;
}
return NO;
}
+ (id)modalFromToken:(NSString *)token;
{
NSDictionary *modalKey = [modalKeyGlobal objectForKey:token];
if (![modalKey isKindOfClass:[NSDictionary class]]) {
return nil;
}
Class wsclass = [WSTransObj createWSObjectClassWithOneDic:modalKey token:token];
id instance = [WSTransObj modalArrWithDictionarys:@[modalKey] andModalClass:wsclass][0];
[WSTransObj modalInitializeWithDic:modalKey WithInstance:instance];
return instance;
}
+ (void)removeToken:(NSString *)token
{
[modalKeyGlobal removeObjectForKey:token];
}
+ (void)removeAllToken
{
[modalKeyGlobal removeAllObjects];
}
+ (void)modalInitializeWithDic:(NSDictionary *)dic WithInstance:(id)instance;
{
NSArray *allKeys = [dic allKeys];
for (NSString *key in allKeys) {
id subIns = dic[key];
if ([subIns isKindOfClass:[NSDictionary class]]) {
[WSTransObj modalInitializeWithDic:subIns WithInstance:[WSTransObj valueGetterOfModal:instance withKey:key]];
}
else{
[WSTransObj valueSetterOfModal:instance withKey:key withValue:nil];
}
}
}
#pragma mark - 获取模型的值
/**
* WSTransObj静态方法创建的对象 获取对象属性的方法
*
* @param key 属性
*
* @return 属性对应的值
*/
- (id)valueForKey:(NSString *)key
{
return [WSTransObj valueGetterOfModal:self withKey:key];
}
#pragma mark - 设置模型的值
/**
* WSTransObj静态方法创建的对象 设置对象属性的值的方法
*
* @param value 需要设置的值
* @param key 设置值的属性
*/
- (void)setValue:(id)value forKey:(NSString *)key
{
return [WSTransObj valueSetterOfModal:self withKey:key withValue:value];
}
- (NSString *)description
{
NSMutableString *mutableStr = [NSMutableString string];
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([self class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
//WSLogA(@"%s %s",name,type);
NSString *key = [NSString stringWithUTF8String:name];
[mutableStr appendFormat:@"\n%@:%@\n",[key substringFromIndex:1],[self valueForKey:[key substringFromIndex:1]]];
}
return mutableStr;
}
@end