单军华
2018-04-20 2197e837490f5083d18d9fdb97265d1e0f04832a
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
//  MJExtensionDemo.h
//  BaseProject
//
//  Created by WindShan on 2016/12/8.
//  Copyright © 2016年 WindShan. All rights reserved.
//
 
#ifndef MJExtensionDemo_h
#define MJExtensionDemo_h
//
//
///**
// MJ友情提醒:
// 1.MJExtension是一套“字典和模型之间互相转换”的轻量级框架
// 2.MJExtension能完成的功能
// * 字典 --> 模型
// * 模型 --> 字典
// * 字典数组 --> 模型数组
// * 模型数组 --> 字典数组
// 3.具体用法主要参考 main.m中各个函数 以及 "NSObject+MJKeyValue.h"
// 4.希望各位大神能用得爽
// */
//int main(int argc, char * argv[]) {
//    @autoreleasepool {
//        // 关于模型的具体配置可以参考:MJExtensionConfig.m
//        // 或者参考每个模型的.m文件中被注释掉的配置
//        
//        execute(keyValues2object, @"简单的字典 -> 模型");
//        execute(keyValues2object1, @"JSON字符串 -> 模型");
//        execute(keyValues2object2, @"复杂的字典 -> 模型 (模型里面包含了模型)");
//        execute(keyValues2object3, @"复杂的字典 -> 模型 (模型的数组属性里面又装着模型)");
//        execute(keyValues2object4, @"简单的字典 -> 模型(key替换,比如ID和id,支持多级映射)");
//        execute(keyValuesArray2objectArray, @"字典数组 -> 模型数组");
//        execute(object2keyValues, @"模型转字典");
//        execute(objectArray2keyValuesArray, @"模型数组 -> 字典数组");
//        execute(coreData, @"CoreData示例");
//        execute(coding, @"NSCoding示例");
//        execute(replacedKeyFromPropertyName121, @"统一转换属性名(比如驼峰转下划线)");
//        execute(newValueFromOldValue, @"过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@"")");
//        execute(logAllProperties, @"使用MJExtensionLog打印模型的所有属性");
//        
//        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
//    }
//}
//
///**
// *  简单的字典 -> 模型
// */
//void keyValues2object()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"name" : @"Jack",
//                           @"icon" : @"lufy.png",
//                           @"age" : @"20",
//                           @"height" : @1.55,
//                           @"money" : @"100.9",
//                           @"sex" : @(SexFemale),
//                           @"gay" : @"1"
//                           //  @"gay" : @"NO"
//                           //  @"gay" : @"true"
//                           };
//    
//    // 2.将字典转为MJUser模型
//    MJUser *user = [MJUser mj_objectWithKeyValues:dict];
//    
//    // 3.打印MJUser模型的属性
//    MJExtensionLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay);
//}
//
///**
// *  JSON字符串 -> 模型
// */
//void keyValues2object1()
//{
//    // 1.定义一个JSON字符串
//    NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20, \"height\":333333.7}";
//    
//    // 2.将JSON字符串转为MJUser模型
//    MJUser *user = [MJUser mj_objectWithKeyValues:jsonString];
//    
//    // 3.打印MJUser模型的属性
//    MJExtensionLog(@"name=%@, icon=%@, age=%d, height=%@", user.name, user.icon, user.age, user.height);
//}
//
///**
// *  复杂的字典 -> 模型 (模型里面包含了模型)
// */
//void keyValues2object2()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"text" : @"是啊,今天天气确实不错!",
//                           
//                           @"user" : @{
//                                   @"name" : @"Jack",
//                                   @"icon" : @"lufy.png"
//                                   },
//                           
//                           @"retweetedStatus" : @{
//                                   @"text" : @"今天天气真不错!",
//                                   
//                                   @"user" : @{
//                                           @"name" : @"Rose",
//                                           @"icon" : @"nami.png"
//                                           }
//                                   }
//                           };
//    
//    // 2.将字典转为Status模型
//    MJStatus *status = [MJStatus mj_objectWithKeyValues:dict];
//    
//    // 3.打印status的属性
//    NSString *text = status.text;
//    NSString *name = status.user.name;
//    NSString *icon = status.user.icon;
//    MJExtensionLog(@"text=%@, name=%@, icon=%@", text, name, icon);
//    
//    // 4.打印status.retweetedStatus的属性
//    NSString *text2 = status.retweetedStatus.text;
//    NSString *name2 = status.retweetedStatus.user.name;
//    NSString *icon2 = status.retweetedStatus.user.icon;
//    MJExtensionLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
//}
//
///**
// *  复杂的字典 -> 模型 (模型的数组属性里面又装着模型)
// */
//void keyValues2object3()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"statuses" : @[
//                                   @{
//                                       @"text" : @"今天天气真不错!",
//                                       
//                                       @"user" : @{
//                                               @"name" : @"Rose",
//                                               @"icon" : @"nami.png"
//                                               }
//                                       },
//                                   
//                                   @{
//                                       @"text" : @"明天去旅游了",
//                                       
//                                       @"user" : @{
//                                               @"name" : @"Jack",
//                                               @"icon" : @"lufy.png"
//                                               }
//                                       }
//                                   
//                                   ],
//                           
//                           @"ads" : @[
//                                   @{
//                                       @"image" : @"ad01.png",
//                                       @"url" : @"http://www.小码哥ad01.com"
//                                       },
//                                   @{
//                                       @"image" : @"ad02.png",
//                                       @"url" : @"http://www.小码哥ad02.com"
//                                       }
//                                   ],
//                           
//                           @"totalNumber" : @"2014",
//                           @"previousCursor" : @"13476589",
//                           @"nextCursor" : @"13476599"
//                           };
//    
//    // 2.将字典转为MJStatusResult模型
//    MJStatusResult *result = [MJStatusResult mj_objectWithKeyValues:dict];
//    
//    // 3.打印MJStatusResult模型的简单属性
//    MJExtensionLog(@"totalNumber=%@, previousCursor=%lld, nextCursor=%lld", result.totalNumber, result.previousCursor, result.nextCursor);
//    
//    // 4.打印statuses数组中的模型属性
//    for (MJStatus *status in result.statuses) {
//        NSString *text = status.text;
//        NSString *name = status.user.name;
//        NSString *icon = status.user.icon;
//        MJExtensionLog(@"text=%@, name=%@, icon=%@", text, name, icon);
//    }
//    
//    // 5.打印ads数组中的模型属性
//    for (MJAd *ad in result.ads) {
//        MJExtensionLog(@"image=%@, url=%@", ad.image, ad.url);
//    }
//}
//
///**
// * 简单的字典 -> 模型(key替换,比如ID和id。多级映射,比如 oldName 和 name.oldName)
// */
//void keyValues2object4()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"id" : @"20",
//                           @"desciption" : @"好孩子",
//                           @"name" : @{
//                                   @"newName" : @"lufy",
//                                   @"oldName" : @"kitty",
//                                   @"info" : @[
//                                           @"test-data",
//                                           @{@"nameChangedTime" : @"2013-08-07"}
//                                           ]
//                                   },
//                           @"other" : @{
//                                   @"bag" : @{
//                                           @"name" : @"小书包",
//                                           @"price" : @100.7
//                                           }
//                                   }
//                           };
//    
//    // 2.将字典转为MJStudent模型
//    MJStudent *stu = [MJStudent mj_objectWithKeyValues:dict];
//    
//    // 3.打印MJStudent模型的属性
//    MJExtensionLog(@"ID=%@, desc=%@, otherName=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.otherName, stu.oldName, stu.nowName, stu.nameChangedTime);
//    MJExtensionLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
//    
//    //    CFTimeInterval begin = CFAbsoluteTimeGetCurrent();
//    //    for (int i = 0; i< 10000; i++) {
//    //        [MJStudent mj_objectWithKeyValues:dict];
//    //    }
//    //    CFTimeInterval end = CFAbsoluteTimeGetCurrent();
//    //    MJExtensionLog(@"%f", end - begin);
//}
//
///**
// *  字典数组 -> 模型数组
// */
//void keyValuesArray2objectArray()
//{
//    // 1.定义一个字典数组
//    NSArray *dictArray = @[
//                           @{
//                               @"name" : @"Jack",
//                               @"icon" : @"lufy.png",
//                               },
//                           
//                           @{
//                               @"name" : @"Rose",
//                               @"icon" : @"nami.png",
//                               }
//                           ];
//    
//    // 2.将字典数组转为MJUser模型数组
//    NSArray *userArray = [MJUser mj_objectArrayWithKeyValuesArray:dictArray];
//    
//    // 3.打印userArray数组中的MJUser模型属性
//    for (MJUser *user in userArray) {
//        MJExtensionLog(@"name=%@, icon=%@", user.name, user.icon);
//    }
//}
//
///**
// *  模型 -> 字典
// */
//void object2keyValues()
//{
//    // 1.新建模型
//    MJUser *user = [[MJUser alloc] init];
//    user.name = @"Jack";
//    user.icon = @"lufy.png";
//    
//    MJStatus *status = [[MJStatus alloc] init];
//    status.user = user;
//    status.text = @"今天的心情不错!";
//    
//    // 2.将模型转为字典
//    NSDictionary *statusDict = status.mj_keyValues;
//    MJExtensionLog(@"%@", statusDict);
//    
//    MJExtensionLog(@"%@", [status mj_keyValuesWithKeys:@[@"text"]]);
//    
//    // 3.新建多级映射的模型
//    MJStudent *stu = [[MJStudent alloc] init];
//    stu.ID = @"123";
//    stu.oldName = @"rose";
//    stu.nowName = @"jack";
//    stu.desc = @"handsome";
//    stu.nameChangedTime = @"2018-09-08";
//    stu.books = @[@"Good book", @"Red book"];
//    
//    MJBag *bag = [[MJBag alloc] init];
//    bag.name = @"小书包";
//    bag.price = 205;
//    stu.bag = bag;
//    
//    NSDictionary *stuDict = stu.mj_keyValues;
//    MJExtensionLog(@"%@", stuDict);
//    MJExtensionLog(@"%@", [stu mj_keyValuesWithIgnoredKeys:@[@"bag", @"oldName", @"nowName"]]);
//    MJExtensionLog(@"%@", stu.mj_JSONString);
//    
//    [MJStudent mj_referenceReplacedKeyWhenCreatingKeyValues:NO];
//    MJExtensionLog(@"\n模型转字典时,字典的key参考replacedKeyFromPropertyName等方法:\n%@", stu.mj_keyValues);
//}
//
///**
// *  模型数组 -> 字典数组
// */
//void objectArray2keyValuesArray()
//{
//    // 1.新建模型数组
//    MJUser *user1 = [[MJUser alloc] init];
//    user1.name = @"Jack";
//    user1.icon = @"lufy.png";
//    
//    MJUser *user2 = [[MJUser alloc] init];
//    user2.name = @"Rose";
//    user2.icon = @"nami.png";
//    
//    NSArray *userArray = @[user1, user2];
//    
//    // 2.将模型数组转为字典数组
//    NSArray *dictArray = [MJUser mj_keyValuesArrayWithObjectArray:userArray];
//    MJExtensionLog(@"%@", dictArray);
//}
//
///**
// *  CoreData示例
// */
//void coreData()
//{
//    NSDictionary *dict = @{
//                           @"name" : @"Jack",
//                           @"icon" : @"lufy.png",
//                           @"age" : @20,
//                           @"height" : @1.55,
//                           @"money" : @"100.9",
//                           @"sex" : @(SexFemale),
//                           @"gay" : @"true"
//                           };
//    
//    // 这个Demo仅仅提供思路,具体的方法参数需要自己创建
//    NSManagedObjectContext *context = nil;
//    MJUser *user = [MJUser mj_objectWithKeyValues:dict context:context];
//    
//    // 利用CoreData保存模型
//    [context save:nil];
//    
//    MJExtensionLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay);
//}
//
///**
// * NSCoding示例
// */
//void coding()
//{
//    // 创建模型
//    MJBag *bag = [[MJBag alloc] init];
//    bag.name = @"Red bag";
//    bag.price = 200.8;
//    
//    NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"bag.data"];
//    // 归档
//    [NSKeyedArchiver archiveRootObject:bag toFile:file];
//    
//    // 解档
//    MJBag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
//    MJExtensionLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
//}
//
///**
// *  统一转换属性名(比如驼峰转下划线)
// */
//void replacedKeyFromPropertyName121()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"nick_name" : @"旺财",
//                           @"sale_price" : @"10.5",
//                           @"run_speed" : @"100.9"
//                           };
//    
//    // 2.将字典转为MJUser模型
//    MJDog *dog = [MJDog mj_objectWithKeyValues:dict];
//    
//    // 3.打印MJUser模型的属性
//    MJExtensionLog(@"nickName=%@, scalePrice=%f runSpeed=%f", dog.nickName, dog.salePrice, dog.runSpeed);
//}
//
///**
// *  过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@"")
// */
//void newValueFromOldValue()
//{
//    // 1.定义一个字典
//    NSDictionary *dict = @{
//                           @"name" : @"5分钟突破iOS开发",
//                           @"publishedTime" : @"2011-09-10"
//                           };
//    
//    // 2.将字典转为MJUser模型
//    MJBook *book = [MJBook mj_objectWithKeyValues:dict];
//    
//    // 3.打印MJUser模型的属性
//    MJExtensionLog(@"name=%@, publisher=%@, publishedTime=%@", book.name, book.publisher, book.publishedTime);
//}
//
///**
// *  使用MJExtensionLog打印模型的所有属性
// */
//void logAllProperties()
//{
//    MJUser *user = [[MJUser alloc] init];
//    user.name = @"MJ";
//    user.age = 10;
//    user.sex = SexMale;
//    user.icon = @"test.png";
//    
//    MJExtensionLog(@"%@", user);
//}
//
//void execute(void (*fn)(), NSString *comment)
//{
//    MJExtensionLog(@"[******************%@******************开始]", comment);
//    fn();
//    MJExtensionLog(@"[******************%@******************结尾]\n ", comment);
//}
#endif /* MJExtensionDemo_h */