Runtime应用一《将API返回的字典转为模型》

LRAOPTest:测试模型

LRAOPTest.h

@interface LRAOPTest : NSObject

@property (nonatomic,copy)   NSString *name;

@property (nonatomic,copy)   NSString *age;

@property (nonatomic,strong) NSArray  *list;

+ (instancetype)modelWithDict:(NSDictionary *)dic;

@end

LRAOPTest.m

#import "LRAOPTest.h"

#import <objc/runtime.h>

const char *kPropertyListKey = "LRPropertyListKey";

@implementation LRAOPTest

- (instancetype)copyWithZone:(NSZone *)zone {

    LRAOPTest *model = [[LRAOPTest allocWithZone:zone] init];

    model.name = self.name;

    model.age  = self.age;

    return model;

}

+ (NSArray *)lr_objcProperty {

    // 获取当前对象关联对象

    NSArray *ptyList = objc_getAssociatedObject(self, kPropertyListKey);

    if (ptyList) {

        return ptyList;

    }

    /*

     ** 调用运行时方法,取得类的属性列表

     ** 获取成员变量: class_copyIvarList(Class  _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount);

     ** 获取方法:    class_copyMethodList(Class  _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount);

     ** 获取属性:    class_copyPropertyList(Class  _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount);

     ** 获取协议:    class_copyProtocolList(Class  _Nullable __unsafe_unretained cls, unsigned int * _Nullable outCount);

     */

    unsigned int outCount = 0;

    /*

     ** 参数1:要获取的类

     ** 参数2:类属性的个数指针

     ** 参数3:所有属性的数组,C语言中数组的名字就是指向第一个元素的地址

     ** tips:retain create copy 的需要release

     */

    objc_property_t *propertyList = class_copyPropertyList([self class], &outCount);

    NSMutableArray  *array        = [NSMutableArray array];

    for (unsigned int i = 0; i < outCount; i++) {

        objc_property_t property   = propertyList[i];

        const char *propertyName   = property_getName(property);

        NSLog(@"propertyName:%s",propertyName);

        NSString   *propertyNameOC = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];

        NSLog(@"propertyNameOC:%@",propertyNameOC);

        [array addObject:propertyNameOC];

    }

    /*

     ** 给当前对象添加关联对象

     ** 参数1:对象self

     ** 参数2:动态添加属性的key

     ** 参数3:动态添加属性值

     ** 参数4:对象的引用关系

     */

    objc_setAssociatedObject(self, kPropertyListKey, array.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    /*

     ** class_copyPropertyList 释放

     */

    free(propertyList);

    return array.copy;

}

+ (instancetype)modelWithDict:(NSDictionary *)dict {

    id objcModel               = [[self alloc] init];

    NSArray *propertyList = [self lr_objcProperty];

    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

        NSLog(@"key:%@--value:%@",key,obj);

        if ([propertyList containsObject:key]) {

            [objcModel setValue:obj forKey:key];

        }

    }];

    return objcModel;

}

调用

 

NSDictionary *dict = @{

                           @"name":@"LR",

                           @"age":@"18",

                           @"list":@[@"我",@"是",@"小",@"仙",@"女"]

                           };

    LRAOPTest *model = [LRAOPTest modelWithDict:dict];

    NSLog(@"name:%@",model.name);

    NSLog(@"age:%@",model.age);

    if ([model.list isKindOfClass:[NSArray class]]) {

        [model.list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            NSLog(@"obj:%@",obj);

        }];

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值