|
| 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 |
0 commit comments