美文网首页RumeTime
runtime 字典转model

runtime 字典转model

作者: 天空中的球 | 来源:发表于2016-05-20 11:17 被阅读94次

想着归档,想着model,突然想到其实我们平常字典转model 的需求其实是更常见的,因此此处也继续笔记下。

依然是先从最原始的字典转model,当然我们有时用 字典快速赋值 是最直接的。

- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *, id> *)keyedValues; 

然而有时,我们还是得类似下面这样:

#import <Foundation/Foundation.h>

@interface UserModel : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

@end

#import "UserModel.h"

NSString *const kNameKey = @"name";
NSString *const kAgeKey = @"age";

@implementation UserModel

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        self.name = dictionary[kNameKey];
        self.age = [dictionary[kAgeKey] integerValue];
    }
    return self;
}

依然还是那种需求,当属性特别多的时候,我们就可以用 rumtime 进行字典转model 就简化啦

#import <Foundation/Foundation.h>

@interface TestModel : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSDictionary * testDic;
@property (nonatomic, strong) NSArray * testArray;

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

@end

#import "TestModel.h"
#import <objc/runtime.h>
#import <objc/message.h>

@implementation TestModel

- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    self = [super init];
    if (self) {
        NSMutableArray * keyArray = [NSMutableArray array];
        unsigned  int outCount = 0 ;
        //获取该类的属性列表 class_copyPropertyList
        objc_property_t * propertys = class_copyPropertyList([self  class], &outCount);
        
        for (int i = 0 ; i < outCount; i++) {
            objc_property_t    property =  propertys[i];
            //获取属性对应的名称 property_getName
            NSString * propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            [keyArray addObject:propertyName];
        }
        //记得要释放
        free(propertys);
        
        for (NSString * key in keyArray) {
            NSArray * keyArray = [dictionary allKeys];
            // 先做一个判断
            if (![keyArray containsObject:key]||[dictionary valueForKey:key] == nil) {
                continue ;
            }
            [self setValue:[dictionary valueForKey:key] forKey:key];
        }
    }
    return self;
}

经过测试是OK的

NSDictionary *testReusltDic = @{@"name":@"Yang",
                                    @"age":@"6",
                                    @"testArray":@[@"1",@"2"],
                                    @"testDic":@{@"one":@"1",@"two":@"2"}};
TestModel * model = [[TestModel alloc] initWithDictionary:testReusltDic];
NSLog(@"name ==== %@, age == %ld, testDic===%@, testArray === %@",model.name,model.age,model.testDic,model.testArray);
// output: name ==== Yang, age == 6, testDic==={one = 1;two = 2;}, testArray === (1, 2)

当然model 也可以通过runtime 转换成字典的。

相关文章

网友评论

    本文标题:runtime 字典转model

    本文链接:https://www.haomeiwen.com/subject/bvfnrttx.html