Skip to content

Commit 11fe909

Browse files
committed
上传JCKit正式版 V1.0.0
1 parent 92196e8 commit 11fe909

414 files changed

Lines changed: 51828 additions & 20 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.

JCKit-正式版/JCKit.xcodeproj/project.pbxproj

Lines changed: 1801 additions & 0 deletions
Large diffs are not rendered by default.

JCKit-正式版/JCKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// NSObject+JCJSON.h
3+
// JCKit
4+
//
5+
// Created by molin on 16/3/21.
6+
// Copyright © 2016年 molin. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSObject (JCJSON)
12+
13+
/**
14+
* 创建模型
15+
*
16+
* @param name JSON文件名
17+
*
18+
* @return 模型对象
19+
*/
20+
+ (instancetype)modelWithJSONName:(NSString *)name;
21+
22+
/**
23+
* 创建模型
24+
*
25+
* @param json JSON
26+
*
27+
* @return 模型对象
28+
*/
29+
+ (instancetype)modelWithJSON:(id)json;
30+
31+
/**
32+
* 获取JSON的数据
33+
*
34+
* @param name JSON文件名
35+
*
36+
* @return NSData
37+
*/
38+
+ (NSData *)dataWithJSONName:(NSString *)name;
39+
40+
/**
41+
将属性集合成Dictionary
42+
*/
43+
- (NSDictionary *)togetherIntoDictionary;
44+
45+
/**
46+
将JSON转换成字典
47+
@param name JSON文件名
48+
@return 字典对象
49+
*/
50+
+ (NSDictionary *)dictionaryWithJSONName:(NSString *)name;
51+
52+
@end
53+
54+
55+
56+
57+
58+
@interface NSArray (JCJSON)
59+
60+
- (NSArray *)modelsWithClass:(Class)cla;
61+
62+
@end
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
//
2+
// NSObject+JCJSON.m
3+
// JCKit
4+
//
5+
// Created by molin on 16/3/21.
6+
// Copyright © 2016年 molin. All rights reserved.
7+
//
8+
9+
#import "NSObject+JCJSON.h"
10+
#import <objc/runtime.h>
11+
12+
@implementation NSObject (JCJSON)
13+
14+
+ (NSDictionary *)dictionaryWithJSONName:(NSString *)name {
15+
NSData *data = [self dataWithJSONName:name];
16+
NSError *err = nil;
17+
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
18+
if (![dic isKindOfClass:[NSDictionary class]]) {
19+
dic = nil;
20+
}
21+
return dic;
22+
}
23+
24+
+ (instancetype)modelWithJSONName:(NSString *)name {
25+
NSData *data = [self dataWithJSONName:name];
26+
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
27+
NSAssert([dic isKindOfClass:[NSDictionary class]], @"📍行号:%d,📍类与方法:%s,📍不满足条件:解析出来的不是字典(Dictionary) ",__LINE__,__func__);
28+
Class className = [self class];
29+
NSObject *model = [className new];
30+
if (!model) {
31+
return nil;
32+
}
33+
[model setValuesForKeysWithDictionary:changeWrongfulValue(dic, [model modelAllPropertys])];
34+
return model;
35+
}
36+
37+
+ (instancetype)modelWithJSON:(id)json {
38+
if (!json || json == (id)kCFNull) {
39+
return nil;
40+
}
41+
NSDictionary *dic = nil;
42+
NSData *jsonData = nil;
43+
if ([json isKindOfClass:[NSDictionary class]]) {
44+
dic = json;
45+
}else if ([json isKindOfClass:[NSString class]]) {
46+
jsonData = [(NSString *)json dataUsingEncoding:NSUTF8StringEncoding];
47+
}else if ([json isKindOfClass:[NSData class]]) {
48+
jsonData = json;
49+
}
50+
if (jsonData) {
51+
dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
52+
if (![dic isKindOfClass:[NSDictionary class]]) {
53+
dic = nil;
54+
}
55+
}
56+
Class className = [self class];
57+
NSObject *model = [className new];
58+
[model setValuesForKeysWithDictionary:changeWrongfulValue(dic, [model modelAllPropertys])]; // 系统的方法
59+
return model;
60+
}
61+
62+
+ (NSData *)dataWithJSONName:(NSString *)name {
63+
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
64+
return [NSData dataWithContentsOfFile:path];
65+
}
66+
67+
68+
- (NSDictionary *)modelAllPropertys {
69+
unsigned int count;
70+
NSMutableDictionary *propertyDic = [NSMutableDictionary new];
71+
objc_property_t *propertys = class_copyPropertyList([self class], &count);
72+
for (int i=0; i<count; i++) {
73+
objc_property_t property = propertys[i];
74+
const char *name = property_getName(property);
75+
NSString *key = [NSString stringWithUTF8String:name];
76+
77+
objc_property_attribute_t *attributes = property_copyAttributeList(property, nil);
78+
objc_property_attribute_t attribute_t = attributes[0];
79+
char *type = (char *)attribute_t.value;
80+
if (*type == '@') {
81+
NSString *t = [NSString stringWithUTF8String:attribute_t.value];
82+
if (!([t rangeOfString:@"NS"].location != NSNotFound)) {
83+
propertyDic[key] = [t substringWithRange:NSMakeRange(2, t.length - 3)];
84+
}
85+
}
86+
free(attributes);
87+
}
88+
free(propertys);
89+
return propertyDic;
90+
}
91+
92+
/**
93+
将属性集合成Dictionary
94+
*/
95+
- (NSDictionary *)togetherIntoDictionary {
96+
NSMutableDictionary *dicM = [NSMutableDictionary new];
97+
unsigned int count;
98+
// 获取指向该类所有属性的指针
99+
objc_property_t *propertys = class_copyPropertyList([self class], &count);
100+
for (int i=0; i<count; i++) {
101+
// 获得该类的一个属性的指针
102+
objc_property_t property = propertys[i];
103+
// 获取属性的名称
104+
const char *name = property_getName(property);
105+
// 将C的字符串转为OC的
106+
NSString *key = [NSString stringWithUTF8String:name];
107+
id value = [self valueForKey:key];
108+
if (value) {
109+
dicM[key] = value;
110+
}
111+
}
112+
free(propertys);
113+
return dicM;
114+
}
115+
116+
117+
static inline NSDictionary * changeWrongfulValue(NSDictionary *dic ,NSDictionary *modelPropertys) {
118+
if (!dic) return nil;
119+
120+
static NSDictionary *legalDic;
121+
static dispatch_once_t onceToken;
122+
dispatch_once(&onceToken, ^{
123+
legalDic = @{@"TRUE" : @(YES),
124+
@"TRUE" : @(YES),
125+
@"True" : @(YES),
126+
@"true" : @(YES),
127+
@"FALSE" : @(NO),
128+
@"False" : @(NO),
129+
@"false" : @(NO),
130+
@"YES" : @(YES),
131+
@"Yes" : @(YES),
132+
@"yes" : @(YES),
133+
@"NO" : @(NO),
134+
@"No" : @(NO),
135+
@"no" : @(NO),
136+
@"NIL" : (id)kCFNull,
137+
@"Nil" : (id)kCFNull,
138+
@"nil" : (id)kCFNull,
139+
@"NULL" : (id)kCFNull,
140+
@"Null" : (id)kCFNull,
141+
@"null" : (id)kCFNull,
142+
@"(NULL)" : (id)kCFNull,
143+
@"(Null)" : (id)kCFNull,
144+
@"(null)" : (id)kCFNull,
145+
@"<NULL>" : (id)kCFNull,
146+
@"<Null>" : (id)kCFNull,
147+
@"<null>" : (id)kCFNull
148+
};
149+
});
150+
151+
NSMutableDictionary *dicM = [NSMutableDictionary new];
152+
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
153+
id legalValue = legalDic[[NSString stringWithFormat:@"%@",obj]];
154+
if (legalValue) {
155+
dicM[key] = legalValue;
156+
}else {
157+
Class className = NSClassFromString(modelPropertys[key]);
158+
if (className) {
159+
NSObject *model = [className new];
160+
[model setValuesForKeysWithDictionary:changeWrongfulValue(obj, [model modelAllPropertys])];
161+
dicM[key] = model;
162+
}else {
163+
dicM[key] = obj;
164+
}
165+
}
166+
}];
167+
return dicM;
168+
}
169+
170+
@end
171+
172+
173+
174+
175+
176+
@implementation NSArray (JCJSON)
177+
178+
- (NSArray *)modelsWithClass:(Class)cla {
179+
NSMutableArray *models = [NSMutableArray new];
180+
for (id data in self) {
181+
if ([data isKindOfClass:[NSDictionary class]]) {
182+
NSObject *model = [cla new];
183+
[model setValuesForKeysWithDictionary:changeWrongfulValue(data, [model modelAllPropertys])];
184+
[models addObject:model];
185+
}else {
186+
[models addObject:data];
187+
}
188+
}
189+
return models;
190+
}
191+
192+
@end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// NSArray+JCBlock.h
3+
// JCKitDemo
4+
//
5+
// Created by 林建川 on 16/10/9.
6+
// Copyright © 2016年 molin. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSArray (JCBlock)
12+
13+
/**
14+
遍历每个元素
15+
*/
16+
- (void)each:(void (^)(id obj))block;
17+
18+
/**
19+
遍历每个元素,block有下标
20+
*/
21+
- (void)eachIndex:(void (^)(id obj, NSInteger index))block;
22+
23+
/**
24+
反序遍历每个元素
25+
*/
26+
- (void)reverseEach:(void (^)(id obj))block;
27+
28+
/**
29+
反序遍历每个元素,block有下标
30+
*/
31+
- (void)reverseEachIndex:(void (^)(id obj, NSInteger index))block;
32+
33+
/**
34+
选择YES时前几个的元素
35+
*/
36+
- (NSArray *)select:(BOOL (^)(id obj, NSInteger index))block;
37+
38+
@end
39+
40+
@interface NSMutableArray (JCBlock)
41+
42+
@end

0 commit comments

Comments
 (0)