ios runtime进行数据解析封装

废话不说直接上代码...代码才是最给力的

#import 

@interface BPQNetworkingResult : NSObject
@property (nonatomic,strong)id Data;
@property (nonatomic,strong)NSNumber *Code;
@property (nonatomic,strong)NSString *Reason;

/*!
 首级数据赋值
 */
+(instancetype)modelWithResponse:(NSDictionary *)response;
/*!
 多层级model赋值
 */
+(instancetype)modelWithResult:(id)result;
/*!
 结果返回正常
 */
BOOL succeed(XDNetworkingResult *result);


@end
#import "BPQNetworkingResult.h"
#import 
@implementation XDNetworkingResult
NSString *runtime_value(id object){
    if (isNull(object)) {
        return @"";
    }
    return toString(object);
}
+(instancetype)modelWithResponse:(NSDictionary *)dictionary{
    unsigned int count = 0;
    id model = [self new];//Code/Data/Reason
    objc_property_t *property = class_copyPropertyList([model class], &count);
    for (int i = 0; i < count; i++) {
        NSString *key = [NSString stringWithUTF8String:property_getName(property[i])];
        NSObject *obj = dictionary[key];
        if ([obj isKindOfClass:[NSString class]]) {
            [model setValue:obj forKey:key];//Data/Reason
        }else if([obj isKindOfClass:[NSNumber class]]){
            [model setValue:@([dictionary[key] boolValue]) forKey:key];//Code
        }else if ([obj isKindOfClass:[NSDictionary class]]){
            [model setValue:obj forKey:key];//以字典形式存入model,在需要的地方用model归档
        }else if ([obj isKindOfClass:[NSArray class]]){
            [model setValue:obj forKey:key];//以数组形式存入model,在需要的地方用model归档
        }
    }
    return model;
}
+(id)modelWithResult:(id)result{
    if ([result isKindOfClass:[NSArray class]]) {
        return [self modelWithArray:result];
    }else if ([result isKindOfClass:[NSDictionary class]]) {
        return [self modelWithDictionary:result];
    }
    return nil;
}
+(id)modelWithArray:(NSArray *)array{
    NSMutableArray *mu = [NSMutableArray new];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        [mu addObject:[self modelWithDictionary:dic]];
    }
    return mu;
}
+(id)modelWithDictionary:(NSDictionary *)dictionary{
    id model = [self new];
    unsigned int count = 0;
    objc_property_t *property = class_copyPropertyList([model class], &count);
    for (int i = 0; i < count; i++) {
        NSString *key = [NSString stringWithUTF8String:property_getName(property[i])];
        NSObject *obj = dictionary[key];
        if ([obj isKindOfClass:[NSString class]]) {
            [model setValue:obj forKey:key];
        }else if([obj isKindOfClass:[NSNumber class]]){
            [model setValue:@([dictionary[key] boolValue]) forKey:key];
        }else if ([obj isKindOfClass:[NSDictionary class]]){
            NSLog(@"%@",obj);
        }else if ([obj isKindOfClass:[NSArray class]]){
            NSArray *arr = (NSArray *)obj;
            [model setValue:[self modelWithArray:arr key:key] forKey:key];
        }
    }
    return model;
}
+(NSArray *)modelWithArray:(NSArray *)array key:(NSString *)key{
    NSMutableArray *mu = [NSMutableArray new];
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        id model = [NSClassFromString(key) new];
        unsigned int count = 0;
        objc_property_t *property = class_copyPropertyList([model class], &count);
        for (int a = 0; a < count; a++) {
            NSString *child_key = [NSString stringWithUTF8String:property_getName(property[a])];
            NSObject *obj = dic[child_key];
            if ([obj isKindOfClass:[NSString class]]) {
                [model setValue:obj forKey:child_key];
            }else if ([obj isKindOfClass:[NSNumber class]]){
                [model setValue:@([dic[child_key] boolValue]) forKey:child_key];
            }else if ([obj isKindOfClass:[NSArray class]]){//三级
                NSArray *arr = (NSArray *)obj;
                [model setValue:[self modelWithArray:arr key:child_key] forKey:child_key];
            }else if ([obj isKindOfClass:[NSDictionary class]]){
                NSLog(@"%@",obj);
            }
        }
        [mu addObject:model];
    }
    return mu;
}
BOOL succeed(BPQNetworkingResult *result){
    if ([result.Code  isEqual: @(0)]) {
        return YES;
    }
    return NO;
}
@end
AB03F75E-A25E-4980-BF79-78DD48BE4DB2.png

你可能感兴趣的:(ios runtime进行数据解析封装)