Skip to content

Commit f99ca89

Browse files
committed
add MJExtension
1 parent 2a6a058 commit f99ca89

79 files changed

Lines changed: 5115 additions & 49 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Apple-GitHub-Codeidea/CoderLN:MJExtension-3.0.14/CoderLN:MJExtension-3.0.13.xcodeproj/project.pbxproj

Lines changed: 113 additions & 21 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// MJExtensionConfig.h
3+
// MJExtensionExample
4+
//
5+
// Created by MJ Lee on 15/4/22.
6+
// Copyright (c) 2015年 小码哥. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface MJExtensionConfig : NSObject
12+
13+
14+
@end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// MJExtensionConfig.m
3+
// MJExtensionExample
4+
//
5+
// Created by MJ Lee on 15/4/22.
6+
// Copyright (c) 2015年 小码哥. All rights reserved.
7+
//
8+
9+
#import "MJExtensionConfig.h"
10+
#import "MJExtension.h"
11+
#import "MJBag.h"
12+
#import "MJUser.h"
13+
#import "MJStatusResult.h"
14+
#import "MJStudent.h"
15+
#import "MJDog.h"
16+
#import "MJBook.h"
17+
18+
@implementation MJExtensionConfig
19+
/**
20+
* 这个方法会在MJExtensionConfig加载进内存时调用一次
21+
*/
22+
+ (void)load
23+
{
24+
#pragma mark 如果使用NSObject来调用这些方法,代表所有继承自NSObject的类都会生效
25+
#pragma mark NSObject中的ID属性对应着字典中的id
26+
[NSObject mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
27+
return @{
28+
@"ID" : @"id"
29+
};
30+
}];
31+
32+
#pragma mark MJUser类的只有name、icon属性参与字典转模型
33+
// [MJUser mj_setupAllowedPropertyNames:^NSArray *{
34+
// return @[@"name", @"icon"];
35+
// }];
36+
// 相当于在MJUser.m中实现了+(NSArray *)mj_allowedPropertyNames方法
37+
38+
#pragma mark MJBag类中的name属性不参与归档
39+
[MJBag mj_setupIgnoredCodingPropertyNames:^NSArray *{
40+
return @[@"name"];
41+
}];
42+
// 相当于在MJBag.m中实现了+(NSArray *)mj_ignoredCodingPropertyNames方法
43+
44+
#pragma mark MJBag类中只有price属性参与归档
45+
// [MJBag mj_setupAllowedCodingPropertyNames:^NSArray *{
46+
// return @[@"price"];
47+
// }];
48+
// 相当于在MJBag.m中实现了+(NSArray *)mj_allowedCodingPropertyNames方法
49+
50+
#pragma mark MJStatusResult类中的statuses数组中存放的是MJStatus模型
51+
#pragma mark MJStatusResult类中的ads数组中存放的是MJAd模型
52+
[MJStatusResult mj_setupObjectClassInArray:^NSDictionary *{
53+
return @{
54+
@"statuses" : @"MJStatus", // @"statuses" : [MJStatus class],
55+
@"ads" : @"MJAd" // @"ads" : [MJAd class]
56+
};
57+
}];
58+
// 相当于在MJStatusResult.m中实现了+(NSDictionary *)mj_objectClassInArray方法
59+
60+
#pragma mark MJStudent中的desc属性对应着字典中的desciption
61+
#pragma mark ....
62+
[MJStudent mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
63+
return @{
64+
@"desc" : @"desciption",
65+
@"oldName" : @"name.oldName",
66+
@"nowName" : @"name.newName",
67+
@"otherName" : @[@"otherName", @"name.newName", @"name.oldName"],
68+
@"nameChangedTime" : @"name.info[1].nameChangedTime",
69+
@"bag" : @"other.bag"
70+
};
71+
}];
72+
// 相当于在MJStudent.m中实现了+(NSDictionary *)mj_replacedKeyFromPropertyName方法
73+
74+
#pragma mark MJDog的所有驼峰属性转成下划线key去字典中取值
75+
[MJDog mj_setupReplacedKeyFromPropertyName121:^NSString *(NSString *propertyName) {
76+
return [propertyName mj_underlineFromCamel];
77+
}];
78+
// 相当于在MJDog.m中实现了+(NSDictionary *)mj_replacedKeyFromPropertyName121:方法
79+
80+
#pragma mark MJBook的日期处理、字符串nil值处理
81+
[MJBook mj_setupNewValueFromOldValue:^id(id object, id oldValue, MJProperty *property) {
82+
if ([property.name isEqualToString:@"publisher"]) {
83+
if (oldValue == nil || [oldValue isKindOfClass:[NSNull class]]) return @"";
84+
} else if (property.type.typeClass == [NSDate class]) {
85+
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
86+
fmt.dateFormat = @"yyyy-MM-dd";
87+
return [fmt dateFromString:oldValue];
88+
}
89+
90+
return oldValue;
91+
}];
92+
// 相当于在MJBook.中实现了- (id)mj_newValueFromOldValue:property:方法
93+
}
94+
@end
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
//
2+
// other.h
3+
// MJExtensionExample
4+
//
5+
// Created by MJ Lee on 15/1/22.
6+
// Copyright (c) 2015年 小码哥. All rights reserved.
7+
//
8+
9+
#ifndef MJExtensionExample_other_h
10+
#define MJExtensionExample_other_h
11+
12+
13+
/** 函数的声明(只用于此示例程序,仅仅是为了演示框架的使用) */
14+
void keyValues2object(void);
15+
void keyValues2object1(void);
16+
void keyValues2object2(void);
17+
void keyValues2object3(void);
18+
void keyValues2object4(void);
19+
void keyValuesArray2objectArray(void);
20+
void object2keyValues(void);
21+
void objectArray2keyValuesArray(void);
22+
void coreData(void);
23+
void coding(void);
24+
void replacedKeyFromPropertyName121(void);
25+
void newValueFromOldValue(void);
26+
void logAllProperties(void);
27+
void execute(void (*fn)(void), NSString *comment);
28+
29+
#endif
30+
31+
32+
33+
/**
34+
35+
10:37:46.793352+0800 MJExtensionExample[8319:103189] [******************简单的字典 -> 模型******************开始]
36+
10:37:46.810547+0800 MJExtensionExample[8319:103189] name=Jack, icon=lufy.png, age=20, height=1.55, money=100.9, sex=1, gay=1
37+
10:37:46.810751+0800 MJExtensionExample[8319:103189] [******************简单的字典 -> 模型******************结尾]
38+
39+
10:37:46.810868+0800 MJExtensionExample[8319:103189] [******************JSON字符串 -> 模型******************开始]
40+
10:37:46.811514+0800 MJExtensionExample[8319:103189] name=Jack, icon=lufy.png, age=20, height=333333.7
41+
10:37:46.811658+0800 MJExtensionExample[8319:103189] [******************JSON字符串 -> 模型******************结尾]
42+
43+
10:37:46.811810+0800 MJExtensionExample[8319:103189] [******************复杂的字典 -> 模型 (模型里面包含了模型)******************开始]
44+
10:37:46.812705+0800 MJExtensionExample[8319:103189] text=是啊,今天天气确实不错!, name=Jack, icon=lufy.png
45+
10:37:46.812868+0800 MJExtensionExample[8319:103189] text2=今天天气真不错!, name2=Rose, icon2=nami.png
46+
10:37:46.813003+0800 MJExtensionExample[8319:103189] [******************复杂的字典 -> 模型 (模型里面包含了模型)******************结尾]
47+
48+
10:37:46.813169+0800 MJExtensionExample[8319:103189] [******************复杂的字典 -> 模型 (模型的数组属性里面又装着模型)******************开始]
49+
10:37:46.814295+0800 MJExtensionExample[8319:103189] totalNumber=2014, previousCursor=13476589, nextCursor=13476599
50+
10:37:46.814470+0800 MJExtensionExample[8319:103189] text=今天天气真不错!, name=Rose, icon=nami.png
51+
10:37:46.814850+0800 MJExtensionExample[8319:103189] text=明天去旅游了, name=Jack, icon=lufy.png
52+
10:37:46.815104+0800 MJExtensionExample[8319:103189] image=ad01.png, url=http://www.%E5%B0%8F%E7%A0%81%E5%93%A5ad01.com
53+
10:37:46.815277+0800 MJExtensionExample[8319:103189] image=ad02.png, url=http://www.%E5%B0%8F%E7%A0%81%E5%93%A5ad02.com
54+
10:37:46.815539+0800 MJExtensionExample[8319:103189] [******************复杂的字典 -> 模型 (模型的数组属性里面又装着模型)******************结尾]
55+
56+
10:37:46.815705+0800 MJExtensionExample[8319:103189] [******************简单的字典 -> 模型(key替换,比如ID和id,支持多级映射)******************开始]
57+
10:37:46.817504+0800 MJExtensionExample[8319:103189] ID=20, desc=好孩子, otherName=lufy, oldName=kitty, nowName=lufy, nameChangedTime=2013-08-07
58+
10:37:46.817670+0800 MJExtensionExample[8319:103189] bagName=小书包, bagPrice=100.700000
59+
10:37:46.817845+0800 MJExtensionExample[8319:103189] [******************简单的字典 -> 模型(key替换,比如ID和id,支持多级映射)******************结尾]
60+
61+
10:37:46.818092+0800 MJExtensionExample[8319:103189] [******************字典数组 -> 模型数组******************开始]
62+
10:37:46.818365+0800 MJExtensionExample[8319:103189] name=Jack, icon=lufy.png
63+
10:37:46.818505+0800 MJExtensionExample[8319:103189] name=Rose, icon=nami.png
64+
10:37:46.818649+0800 MJExtensionExample[8319:103189] [******************字典数组 -> 模型数组******************结尾]
65+
66+
10:37:46.818822+0800 MJExtensionExample[8319:103189] [******************模型转字典******************开始]
67+
10:37:46.819710+0800 MJExtensionExample[8319:103189] {
68+
text = "\U4eca\U5929\U7684\U5fc3\U60c5\U4e0d\U9519\Uff01";
69+
user = {
70+
age = 0;
71+
gay = 0;
72+
icon = "lufy.png";
73+
name = Jack;
74+
sex = 0;
75+
};
76+
}
77+
10:37:46.819929+0800 MJExtensionExample[8319:103189] {
78+
text = "\U4eca\U5929\U7684\U5fc3\U60c5\U4e0d\U9519\Uff01";
79+
}
80+
10:37:46.820574+0800 MJExtensionExample[8319:103189] {
81+
books = (
82+
"Good book",
83+
"Red book"
84+
);
85+
desciption = handsome;
86+
id = 123;
87+
name = {
88+
info = (
89+
"<null>",
90+
{
91+
nameChangedTime = "2018-09-08";
92+
}
93+
);
94+
newName = jack;
95+
oldName = rose;
96+
};
97+
other = {
98+
bag = {
99+
name = "\U5c0f\U4e66\U5305";
100+
price = 205;
101+
};
102+
};
103+
}
104+
10:37:46.820878+0800 MJExtensionExample[8319:103189] {
105+
books = (
106+
"Good book",
107+
"Red book"
108+
);
109+
desciption = handsome;
110+
id = 123;
111+
name = {
112+
info = (
113+
"<null>",
114+
{
115+
nameChangedTime = "2018-09-08";
116+
}
117+
);
118+
};
119+
}
120+
10:37:46.821454+0800 MJExtensionExample[8319:103189] {"id":"123","other":{"bag":{"name":"小书包","price":205}},"books":["Good book","Red book"],"name":{"newName":"jack","oldName":"rose","info":[null,{"nameChangedTime":"2018-09-08"}]},"desciption":"handsome"}
121+
10:37:46.821841+0800 MJExtensionExample[8319:103189]
122+
模型转字典时,字典的key参考replacedKeyFromPropertyName等方法:
123+
{
124+
ID = 123;
125+
bag = {
126+
name = "\U5c0f\U4e66\U5305";
127+
price = 205;
128+
};
129+
books = (
130+
"Good book",
131+
"Red book"
132+
);
133+
desc = handsome;
134+
nameChangedTime = "2018-09-08";
135+
nowName = jack;
136+
oldName = rose;
137+
}
138+
10:37:46.821998+0800 MJExtensionExample[8319:103189] [******************模型转字典******************结尾]
139+
140+
10:37:46.822140+0800 MJExtensionExample[8319:103189] [******************模型数组 -> 字典数组******************开始]
141+
10:37:46.822807+0800 MJExtensionExample[8319:103189] (
142+
{
143+
age = 0;
144+
gay = 0;
145+
icon = "lufy.png";
146+
name = Jack;
147+
sex = 0;
148+
},
149+
{
150+
age = 0;
151+
gay = 0;
152+
icon = "nami.png";
153+
name = Rose;
154+
sex = 0;
155+
}
156+
)
157+
10:37:46.823033+0800 MJExtensionExample[8319:103189] [******************模型数组 -> 字典数组******************结尾]
158+
159+
10:37:46.823381+0800 MJExtensionExample[8319:103189] [******************CoreData示例******************开始]
160+
10:37:46.823937+0800 MJExtensionExample[8319:103189] name=Jack, icon=lufy.png, age=20, height=1.55, money=100.9, sex=1, gay=1
161+
10:37:46.824124+0800 MJExtensionExample[8319:103189] [******************CoreData示例******************结尾]
162+
163+
10:37:46.824681+0800 MJExtensionExample[8319:103189] [******************NSCoding示例******************开始]
164+
10:37:46.826849+0800 MJExtensionExample[8319:103189] name=(null), price=200.800000
165+
10:37:46.827046+0800 MJExtensionExample[8319:103189] [******************NSCoding示例******************结尾]
166+
167+
10:37:46.827982+0800 MJExtensionExample[8319:103189] [******************统一转换属性名(比如驼峰转下划线)******************开始]
168+
10:37:46.828863+0800 MJExtensionExample[8319:103189] nickName=旺财, scalePrice=10.500000 runSpeed=100.900000
169+
10:37:46.829026+0800 MJExtensionExample[8319:103189] [******************统一转换属性名(比如驼峰转下划线)******************结尾]
170+
171+
10:37:46.829150+0800 MJExtensionExample[8319:103189] [******************过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@)******************开始]
172+
10:37:46.832763+0800 MJExtensionExample[8319:103189] name=5分钟突破iOS开发, publisher=, publishedTime=Sat Sep 10 00:00:00 2011
173+
10:37:46.832929+0800 MJExtensionExample[8319:103189] [******************过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@)******************结尾]
174+
175+
10:37:46.833046+0800 MJExtensionExample[8319:103189] [******************使用MJExtensionLog打印模型的所有属性******************开始]
176+
10:37:46.833850+0800 MJExtensionExample[8319:103189] {
177+
age = 10;
178+
gay = 0;
179+
icon = "test.png";
180+
name = MJ;
181+
sex = 0;
182+
}
183+
10:37:46.834022+0800 MJExtensionExample[8319:103189] [******************使用MJExtensionLog打印模型的所有属性******************结尾]
184+
185+
*/
186+
187+
188+
189+
190+
191+
192+
193+

0 commit comments

Comments
 (0)