iOS开发 字典->模型(runtime)

1.新建NSObject的分类(Model)
2.在NSObject+Model.h文件中声明一下函数:

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

3.在NSObject+Model.m文件中实现函数,代码如下:

#import "NSObject+Model.h"
#import <objc/message.h>
@implementation NSObject (Model)
+ (instancetype)modelWithDict:(NSDictionary *)dict
{
    // 创建一个模型对象(由于不知道模型具体的类型,选择使用id)
    id objc = [[self alloc] init];
    
    unsigned int count = 0;
    
    // 获取成员变量数组
    Ivar *ivarList = class_copyIvarList(self, &count);
    
    // 遍历所有成员变量
    for (int i = 0; i < count; i++) {
        
        // 获取成员变量
        Ivar ivar = ivarList[i];
        
        // 获取成员变量名称
        NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
        
        // 获取成员变量类型
        NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
        
        // 注:此时输出的type == @"@\"User\"";需转换为@"User"
        
        //  @"@\"User\"" -> @"User"
        type = [type stringByReplacingOccurrencesOfString:@"@\"" withString:@""];
        type = [type stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        
        // 成员变量名称转换key  _user===>user
        NSString *key = [ivarName substringFromIndex:1];
        
        // 从字典中取出对应value dict[@"user"] -> 字典
        id value = dict[key];
        
        // 二级转换
        // 当获取的value为字典时,且type是模型
        // 即二级模型
        if ([value isKindOfClass:[NSDictionary class]] && ![type containsString:@"NS"]) {
            
            // 获取对应的类
            Class className = NSClassFromString(type);
            
            // 字典转模型
            value = [className modelWithDict:value];
            
        }
        // 给模型中属性赋值
        if (value) {
            [objc setValue:value forKey:key];
        }
        
    }
    
    return objc;
}
@end

4.使用在要使用的文件中导入NSObject+Model.h文件,创建一个测试的继承NSobject的TestModel类
下面是测试的代码:
TestModel.h

#import "NSObject+Model.h"

@interface TestModel : NSObject
@property (nonatomic,strong) NSString *avatarUrl;
@property (nonatomic,strong) NSString *nickname;
@property (nonatomic,strong) NSString *content;
@property (nonatomic,strong) NSString *type;
@property (nonatomic,strong) NSString *time;
@end

使用,代码如下:

-(void)test{
    NSDictionary *dic=@{
                        @"avatarUrl":@"http://img.zcool.cn/community/017274582000cea84a0e282b576a32.jpg",
                        @"nickname":@"nickname",
                        @"content":@"input you nickname",
                        @"type":@"1",
                        @"time":@"2017-12-10"
                        };
    TestModel *model =[TestModel modelWithDict:dic];
   
    NSLog(@"%@",model.nickname);
    
}

参考链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,857评论 0 9
  • 简书作者(楠天下的太阳花)授权发布此文章。 01 前面有文章,说到变现能力。今天特别讲技能的变现,用金融术语,就是...
    投资家说阅读 416评论 0 2
  • 06 谈及三毛,我的心里总是有些复杂。 我爱她的文字,高中时代那些辗转反侧的夜里我就会翻开她的书来,好像失眠患者定...
    大病初愈阅读 550评论 1 5
  • 今天下午,儿子要去学校学习,我像往常一样早早穿好衣服,拿上车钥匙,准备开车送儿子去。 出乎意料,儿子坚决...
    狄克先生阅读 311评论 0 9