123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- //
- // SPSBBaseNetworkModel.m
- // 我的社保
- //
- // Created by shanp on 2021/4/20.
- //
- #import "SPSBBaseNetworkModel.h"
- #import <objc/runtime.h>
- #import <JXHGeneralTools.h>
- @implementation SPSBBaseNetworkModel
- #define spsb_keyPrefix 5
- #pragma mark - 字典转Model
- + (NSArray *)getModelListWithArray:(nullable NSArray *)array {
- if (!array || ![array isKindOfClass:[NSArray class]]) {
- return @[];
- }
- NSMutableArray *dataList = NSMutableArray.new;
- for (id obj in array) {
- if (![obj isKindOfClass:[NSDictionary class]]) {
- continue;
- }
- id data = self.new;
- bool flag = [data handleDataWithDictionary:obj];
- if (flag) {
- [dataList addObject:data];
- }
- }
- return dataList;
- }
- + (id)getModelWithDictionary:(nullable NSDictionary *)dict {
- id data = self.new;
- if (!dict || ![dict isKindOfClass:[NSDictionary class]]) {
- dict = @{};
- }
- if ([data handleDataWithDictionary:dict]) {
- return data;
- }
- [data handleDataWithDictionary:@{}];
- return data;
- }
- - (bool)handleDataWithDictionary:(nullable NSDictionary *)dict {
- Class class = [self class];
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb_"]) {
- SEL action = NSSelectorFromString([NSString stringWithFormat:@"GET%@WithDictionary:key:", key]);
- IMP imp = [self methodForSelector:action];
- id (*func)(id, SEL, NSDictionary *, NSString *) = (void *)imp;
- id value = func(self, action, dict, [key substringFromIndex:spsb_keyPrefix]);
- [self setValue:value forKey:key];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
-
- return [self handleAdditionalProperty];
- }
- - (bool)handleAdditionalProperty {
- return true;
- }
- - (id)getStringDataWithDictionary:(nullable NSDictionary *)dic variable:(NSString *)variable {
- if (!dic || !dic[variable] || jxh_isNullClass(dic[variable])) {
- return @"";
- }
- return [NSString stringWithFormat:@"%@", dic[variable]];
- }
- #pragma mark - Model转字典
- + (NSArray *)getArray:(NSArray *)array {
- if (!array || ![array isKindOfClass:[NSArray class]]) {
- return @[];
- }
- NSMutableArray *result = NSMutableArray.new;
- for (id object in array) {
- if (![object isKindOfClass:[self class]]) continue;
- id r = [object performSelector:@selector(getDictionary)];
- [result addObject:r];
- }
- return result;
- }
- - (NSDictionary *)getDictionary {
- NSMutableDictionary *dic = NSMutableDictionary.new;
- Class class = [self class];
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb_"]) {
- SEL action = NSSelectorFromString([NSString stringWithFormat:@"GETStringWith%@:", key]);
- IMP imp = [self methodForSelector:action];
- id (*func)(id, SEL, NSString *) = (void *)imp;
- id value = func(self, action, key);
- [dic setValue:value forKey:[key substringFromIndex:spsb_keyPrefix]];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
- return [self handleAdditionalWithDic:dic];
- }
- - (NSMutableDictionary *)handleAdditionalWithDic:(NSMutableDictionary *)dic {
- return dic;
- }
- - (id)getStringWithKey:(NSString *)key {
- return [self valueForKey:key];
- }
- #pragma mark - 消息转发
- + (BOOL)resolveInstanceMethod:(SEL)sel {
- NSString *method = NSStringFromSelector(sel);
- if ([method hasPrefix:@"GETspsb_"]) {
- Method originalMethod = class_getInstanceMethod(self, @selector(getStringDataWithDictionary:variable:));
- class_addMethod(self, sel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
-
- return true;
- }
- if ([method hasPrefix:@"GETStringWithspsb_"]) {
- Method originalMethod = class_getInstanceMethod(self, @selector(getStringWithKey:));
- class_addMethod(self, sel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
-
- return true;
- }
- return [super resolveInstanceMethod:sel];
- }
- #pragma mark - 归档
- - (id)copyWithZone:(NSZone *)zone {
- Class class = [self class];
- id new = class.new;
-
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb"]) {
- [new setValue:[self valueForKey:key] forKey:key];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
-
- return new;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- Class class = [self class];
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb"]) {
- [aCoder encodeObject:[self valueForKey:key] forKey:[NSString stringWithFormat:@"k%@", key]];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
-
- }
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super init];
- if (self) {
- Class class = [self class];
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb"]) {
- id value = [aDecoder decodeObjectForKey:[NSString stringWithFormat:@"k%@", key]];
- if (!value) {
- value = @"";
- }
- [self setValue:value forKey:key];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
-
- }
- return self;
- }
- - (NSString *)description {
- NSMutableString *desc = [NSMutableString stringWithString:@""];
- Class class = [self class];
- while ([NSStringFromClass(class) hasPrefix:@"SPSB"]) {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for(int i = 0; i < count; i++) {
- objc_property_t property = properties[i];
- NSString *key = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
- if ([key hasPrefix:@"spsb"]) {
- [desc appendFormat:@"%@ : %@\n", key, [self valueForKey:key]];
- }
- }
- free(properties);
- class = class_getSuperclass(class);
- }
-
- return desc;
- }
- @end
|