Skip to content

Commit 77c6392

Browse files
committed
model 与 item
1 parent 76487f7 commit 77c6392

5 files changed

Lines changed: 427 additions & 0 deletions

File tree

KtTableView/KtTableView.xcodeproj/project.pbxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
E1621E1B1CBDF267003D9438 /* KtTableViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E1A1CBDF267003D9438 /* KtTableViewTests.m */; };
2626
E1621E261CBDF267003D9438 /* KtTableViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = E1621E251CBDF267003D9438 /* KtTableViewUITests.m */; };
2727
E19B41191CC21555008A3E7F /* KtTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E19B41181CC21555008A3E7F /* KtTableViewController.m */; };
28+
E1DCC9EF1CE5A61A00CDC83D /* KtBaseModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DCC9EE1CE5A61A00CDC83D /* KtBaseModel.m */; };
2829
E1DCC9F21CE5A6A000CDC83D /* KtBaseServerAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DCC9F11CE5A6A000CDC83D /* KtBaseServerAPI.m */; };
30+
E1DCC9F81CE5A81000CDC83D /* KtBaseItem.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DCC9F71CE5A81000CDC83D /* KtBaseItem.m */; };
2931
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */; };
3032
/* End PBXBuildFile section */
3133

@@ -84,8 +86,12 @@
8486
E1621E271CBDF267003D9438 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8587
E19B41171CC21555008A3E7F /* KtTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtTableViewController.h; sourceTree = "<group>"; };
8688
E19B41181CC21555008A3E7F /* KtTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtTableViewController.m; sourceTree = "<group>"; };
89+
E1DCC9ED1CE5A61A00CDC83D /* KtBaseModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseModel.h; sourceTree = "<group>"; };
90+
E1DCC9EE1CE5A61A00CDC83D /* KtBaseModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseModel.m; sourceTree = "<group>"; };
8791
E1DCC9F01CE5A6A000CDC83D /* KtBaseServerAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseServerAPI.h; sourceTree = "<group>"; };
8892
E1DCC9F11CE5A6A000CDC83D /* KtBaseServerAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseServerAPI.m; sourceTree = "<group>"; };
93+
E1DCC9F61CE5A81000CDC83D /* KtBaseItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseItem.h; sourceTree = "<group>"; };
94+
E1DCC9F71CE5A81000CDC83D /* KtBaseItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseItem.m; sourceTree = "<group>"; };
8995
E1EE5C9B1CBE811C0095688B /* KtBaseTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KtBaseTableView.h; sourceTree = "<group>"; };
9096
E1EE5C9C1CBE811C0095688B /* KtBaseTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KtBaseTableView.m; sourceTree = "<group>"; };
9197
/* End PBXFileReference section */
@@ -181,6 +187,7 @@
181187
isa = PBXGroup;
182188
children = (
183189
E12A4F591CE8180E00AB6F35 /* KtExtension */,
190+
E1DCC9EC1CE5A5FA00CDC83D /* KtBaseModel */,
184191
E108B2A81CBE148200191E0B /* KtBaseTableViewController */,
185192
E1621E031CBDF267003D9438 /* AppDelegate.h */,
186193
E1621E041CBDF267003D9438 /* AppDelegate.m */,
@@ -228,6 +235,10 @@
228235
E1DCC9EC1CE5A5FA00CDC83D /* KtBaseModel */ = {
229236
isa = PBXGroup;
230237
children = (
238+
E1DCC9ED1CE5A61A00CDC83D /* KtBaseModel.h */,
239+
E1DCC9EE1CE5A61A00CDC83D /* KtBaseModel.m */,
240+
E1DCC9F61CE5A81000CDC83D /* KtBaseItem.h */,
241+
E1DCC9F71CE5A81000CDC83D /* KtBaseItem.m */,
231242
E1DCC9F01CE5A6A000CDC83D /* KtBaseServerAPI.h */,
232243
E1DCC9F11CE5A6A000CDC83D /* KtBaseServerAPI.m */,
233244
);
@@ -424,12 +435,14 @@
424435
buildActionMask = 2147483647;
425436
files = (
426437
E12A4F5E1CE8180E00AB6F35 /* NSDictionary+KtExtension.m in Sources */,
438+
E1DCC9F81CE5A81000CDC83D /* KtBaseItem.m in Sources */,
427439
E19B41191CC21555008A3E7F /* KtTableViewController.m in Sources */,
428440
E12A4F5F1CE8180E00AB6F35 /* UIView+KtExtension.m in Sources */,
429441
E108B2B41CBE1C9100191E0B /* KtMainTableViewDataSource.m in Sources */,
430442
E108B2AB1CBE14C300191E0B /* KtTableViewDataSource.m in Sources */,
431443
E108B2B71CBE1F6500191E0B /* KtTableViewBaseItem.m in Sources */,
432444
E108B2B11CBE191300191E0B /* KtBaseTableViewCell.m in Sources */,
445+
E1DCC9EF1CE5A61A00CDC83D /* KtBaseModel.m in Sources */,
433446
E1DCC9F21CE5A6A000CDC83D /* KtBaseServerAPI.m in Sources */,
434447
E1621E081CBDF267003D9438 /* KTMainViewController.m in Sources */,
435448
E1EE5C9D1CBE811C0095688B /* KtBaseTableView.m in Sources */,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// KtBaseItem.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface KtBaseItem : NSObject
12+
13+
- (id)initWithData:(NSDictionary *)data;
14+
15+
// property中如有包含TBCBaseListItem对象的数组,需要设定此规则
16+
- (void)addMappingRuleArrayProperty:(NSString*)propertyName class:(Class)cls;
17+
// 所有需要映射的property都需要设定此规则
18+
- (void)addMappingRuleProperty:(NSString*)propertyName pathInJson:(NSString*)path;
19+
20+
@end
21+
22+
@interface KtClassHelper : NSObject
23+
24+
+ (KtClassHelper *)sharedInstance;
25+
26+
@property (nonatomic, retain) NSMutableDictionary *propertyListCache;
27+
28+
- (NSDictionary *)propertyList:(Class)cls;
29+
30+
@end
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
//
2+
// KtBaseItem.m
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "KtBaseItem.h"
10+
#import <objc/runtime.h>
11+
#import "NSDictionary+KtExtension.h"
12+
13+
@interface KtBaseItem ()
14+
15+
@property (strong, nonatomic) NSMutableDictionary *jsonDataMap;
16+
@property (strong, nonatomic) NSMutableDictionary *jsonArrayClassMap;
17+
18+
@end
19+
20+
@implementation KtBaseItem
21+
22+
- (id)init {
23+
if (self = [super init]) {
24+
_jsonDataMap = [[NSMutableDictionary alloc] init];
25+
_jsonArrayClassMap = [[NSMutableDictionary alloc] init];
26+
}
27+
return self;
28+
}
29+
30+
- (id)initWithData:(NSDictionary *)data {
31+
if (self = [self init]) {
32+
[self setData:data];
33+
}
34+
return self;
35+
}
36+
37+
- (id)setData:(id)data {
38+
if (nil == data) {
39+
return self;
40+
}
41+
[self parseData:data];
42+
return self;
43+
}
44+
45+
- (void)parseData:(NSDictionary *)data {
46+
Class cls = [self class];
47+
while (cls != [KtBaseItem class]) {
48+
NSDictionary *propertyList = [[KtClassHelper sharedInstance] propertyList:cls];
49+
for (NSString *key in [propertyList allKeys]) {
50+
NSString *typeString = [propertyList objectForKey:key];
51+
NSString* path = [self.jsonDataMap objectForKey:key];
52+
id value = [data objectAtPath:path];
53+
54+
[self setfieldName:key fieldClassName:typeString value:value];
55+
}
56+
cls = class_getSuperclass(cls);
57+
}
58+
}
59+
60+
- (void)setfieldName:(NSString*)name fieldClassName:(NSString*)className value:(id)value {
61+
if (value == nil) {
62+
return;
63+
}
64+
//如果结构里嵌套了TBCBaseListItem 也解析
65+
if ([NSClassFromString(className) isSubclassOfClass:[KtBaseItem class]]) {
66+
Class entityClass = NSClassFromString(className);
67+
if ([value isKindOfClass:[NSString class]]) {
68+
NSString *str = (NSString *)value;
69+
if (str && str.length == 0) {
70+
return;
71+
}
72+
}
73+
if ([value isKindOfClass:[NSArray class]]) {
74+
NSArray *arr = (NSArray *)value;
75+
if (arr && [arr count] == 0) {
76+
return;
77+
}
78+
}
79+
if (entityClass) {
80+
KtBaseItem* entityInstance = [[entityClass alloc] init];
81+
[entityInstance parseData:value];
82+
[self setValue:entityInstance forKey:name];
83+
}
84+
}
85+
else if (![value isKindOfClass:NSClassFromString(className)])
86+
{
87+
if ([value isKindOfClass:[NSString class]] && NSClassFromString(className) == [NSNumber class]) {
88+
[self setValue:[NSNumber numberWithInteger:[(NSString *)value integerValue]] forKey:name];
89+
}else if ([value isKindOfClass:[NSNumber class]] && NSClassFromString(className) == [NSString class]){
90+
[self setValue:[(NSNumber *)value stringValue] forKey:name];
91+
}
92+
return;
93+
}
94+
//如果是array判断array内类型
95+
else if ([NSClassFromString(className) isSubclassOfClass:[NSArray class]])
96+
{
97+
NSString* typeName = [_jsonArrayClassMap objectForKey:name];
98+
if (typeName)
99+
{
100+
//json中不是array 类型错误
101+
if (![value isKindOfClass:[NSArray class]]) {
102+
return;
103+
}
104+
Class entityClass = NSClassFromString(typeName);
105+
//entiyClass不存在
106+
if (!entityClass) {
107+
return;
108+
}
109+
//entiyClass不是TBCJsonEntityBase的子类
110+
if (![entityClass isSubclassOfClass:[KtBaseItem class]]) {
111+
return;
112+
}
113+
NSMutableArray* mutableArr = [[NSMutableArray alloc] initWithCapacity:[(NSArray*)value count]];
114+
for (NSDictionary*dict in (NSArray*)value ) {
115+
//arry中存的不是dict
116+
if (![dict isKindOfClass:[NSDictionary class]]) {
117+
return;
118+
}
119+
KtBaseItem* entityInstance = [[entityClass alloc] init];
120+
121+
[entityInstance parseData:dict];
122+
if (entityInstance) {
123+
[mutableArr addObject:entityInstance];
124+
}
125+
}
126+
[self setValue:mutableArr forKey:name];
127+
}
128+
else
129+
{
130+
[self setValue:value forKey:name];
131+
}
132+
}
133+
//正常情况
134+
else
135+
{
136+
[self setValue:value forKey:name];
137+
}
138+
}
139+
140+
#pragma -mark 映射规则
141+
- (void)addMappingRuleProperty:(NSString*)propertyName pathInJson:(NSString*)path {
142+
[_jsonDataMap setObject:path forKey:propertyName];
143+
}
144+
145+
- (NSString *)getPahtForDataMapWithKey:(NSString *)aKey {
146+
if (aKey == nil) {
147+
return nil;
148+
}
149+
return [_jsonDataMap objectForKey:aKey];
150+
}
151+
152+
- (NSString *)mappingRuleWithKey:(NSString *)aKey {
153+
if (aKey == nil) {
154+
return nil;
155+
}
156+
return [_jsonArrayClassMap objectForKey:aKey];
157+
}
158+
159+
- (void)addMappingRuleArrayProperty:(NSString*)propertyName class:(Class)cls {
160+
[_jsonArrayClassMap setObject:NSStringFromClass(cls) forKey:propertyName];
161+
}
162+
163+
@end
164+
165+
@implementation KtClassHelper
166+
{
167+
NSRecursiveLock *_propertyListCacheLock;
168+
}
169+
170+
+ (void)load
171+
{
172+
[self sharedInstance];
173+
}
174+
175+
176+
+ (KtClassHelper *)sharedInstance {
177+
static dispatch_once_t once;
178+
static KtClassHelper * singleton;
179+
dispatch_once( &once, ^{ singleton = [[KtClassHelper alloc] init]; } );
180+
return singleton;
181+
}
182+
183+
- (instancetype)init
184+
{
185+
self = [super init];
186+
if (self) {
187+
self.propertyListCache = [NSMutableDictionary dictionary];
188+
_propertyListCacheLock = [[NSRecursiveLock alloc] init];
189+
}
190+
return self;
191+
}
192+
193+
- (NSDictionary *)propertyList:(Class)cls
194+
{
195+
if (cls == NULL){
196+
return nil;
197+
}
198+
199+
[_propertyListCacheLock lock];
200+
201+
NSString *clsName = NSStringFromClass(cls);
202+
NSDictionary *cachePropertyList = [self.propertyListCache objectForKey:clsName];
203+
if (cachePropertyList) {
204+
[_propertyListCacheLock unlock];
205+
return cachePropertyList;
206+
}
207+
208+
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
209+
unsigned int propertyCount = 0;
210+
objc_property_t *propertyList = class_copyPropertyList(cls, &propertyCount);//获取cls 类成员变量列表
211+
for (unsigned i = 0; i < propertyCount; i++) {
212+
objc_property_t property = propertyList[i];
213+
const char *attr = property_getAttributes(property); //取得这个变量的类型
214+
NSString *attrString = [NSString stringWithUTF8String:attr];
215+
NSString *typeAttr = [[attrString componentsSeparatedByString:@","] objectAtIndex:0];
216+
if(typeAttr.length < 8) continue;
217+
NSString *typeString = [typeAttr substringWithRange:NSMakeRange(3, typeAttr.length - 4)];
218+
NSString *key = [NSString stringWithUTF8String:property_getName(property)];//取得这个变量的名称
219+
[dict setObject:typeString forKey:key];
220+
}
221+
free(propertyList);
222+
[self.propertyListCache setObject:dict forKey:clsName];
223+
224+
[_propertyListCacheLock unlock];
225+
return dict;
226+
}
227+
228+
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// KtBaseModel.h
3+
// KtTableView
4+
//
5+
// Created by baidu on 16/5/13.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
#import "KtBaseServerAPI.h"
12+
#import "KtBaseItem.h"
13+
14+
@class KtBaseModel;
15+
16+
/*!
17+
@brief 用于KtBaseModel回调的block定义
18+
*/
19+
typedef void(^KtModelBlock)(KtBaseModel *);
20+
21+
@interface KtBaseModel : NSObject
22+
23+
//自动解析的数据类型 可能在不同线程访问 因此设置为 atomic
24+
@property (assign,atomic) Class parseDataClassType;
25+
26+
// 回调函数
27+
@property (nonatomic, copy) KtModelBlock completionBlock;
28+
29+
//网络请求
30+
@property (nonatomic,retain) KtBaseServerAPI *serverApi;
31+
32+
//网络请求参数
33+
@property (nonatomic,retain) NSDictionary *params;
34+
35+
//请求地址 需要在子类init中初始化
36+
@property (nonatomic,copy) NSString *address;
37+
38+
- (instancetype)initWithAddress:(NSString *)address;
39+
- (void)handleParsedData:(KtBaseItem *)parsedData;
40+
41+
- (void)loadWithShortConnection;
42+
- (void)loadWithLongConnection;
43+
- (void)refresh;
44+
- (void)cancel;
45+
46+
@end

0 commit comments

Comments
 (0)