iOS runtime进行数据解析封装

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

#import <Foundation/Foundation.h>

@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 <objc/runtime.h>
@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

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值