美文网首页iOS DeveloperiOS 开发
iOS --- 使用runtime将JSON转换成Model

iOS --- 使用runtime将JSON转换成Model

作者: icetime17 | 来源:发表于2016-07-26 22:34 被阅读279次

这里简单介绍如何使用runtime将JSON转换成Model.

封装initWithNSDictionary:方法

该方法接收NSDictionary对象, 返回PersonModel对象.

#pragma mark - 使用runtime将JSON转成Model

- (void)json2Model {
    NSString *file = [[NSBundle mainBundle] pathForResource:@"Persons" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    for (NSDictionary *model in array) {
        PersonModel *person = [[PersonModel alloc] initWithNSDictionary:model];
        NSLog(@"%@, %ld, %@, %@", person.name, (long)person.age, person.city, person.job);
    }
}

使用runtime实现

PersonModel的头文件如下:

#import <Foundation/Foundation.h>

@interface PersonModel : NSObject

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

- (instancetype)initWithNSDictionary:(NSDictionary *)dict;

@end

实现文件:

#import "PersonModel.h"
#import <objc/runtime.h>

@implementation PersonModel

- (instancetype)initWithNSDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self) {
        [self prepareModel:dict];
    }
    return self;
}

- (void)prepareModel:(NSDictionary *)dict {
    NSMutableArray *keys = [[NSMutableArray alloc] init];

    u_int count = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = properties[i];
        const char *propertyCString = property_getName(property);
        NSString *propertyName = [NSString stringWithCString:propertyCString encoding:NSUTF8StringEncoding];
        [keys addObject:propertyName];
    }
    free(properties);

    for (NSString *key in keys) {
        if ([dict valueForKey:key]) {
            [self setValue:[dict valueForKey:key] forKey:key];
        }
    }
}

@end

其中的代码也很简单:
使用class_copyPropertyList获取Model的所有属性列表, 遍历该列表使用property_getName即可得到所有属性名.
对于PersonModel中定义的属性, 使用KVC即可将dict中的值赋给该属性.

Demo

Demo请参考:
iOS-RuntimeDemo.

相关文章

  • iOS --- 使用runtime将JSON转换成Model

    这里简单介绍如何使用runtime将JSON转换成Model. 封装initWithNSDictionary:方法...

  • iOS开发之 Runtime模型与字典互转

    根据前面的学习iOS开发之使用Runtime给Model类赋值和OC和Swift中的Runtime,总结一下将字典...

  • NSString、NSArray、NSDictionary转JS

    关于iOS开发,如何将NSString、NSArray、NSDictionary数据转换成JSON数据,然后再使用...

  • iOS-0 一些

    1 iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值 - 简书 2 iOS runti...

  • 使用runtime json转model

    自己最近在学习runtime,希望在学习过程中做些记录,方便自己后来温习查阅。也希望能帮助到别的同学。 在你的mo...

  • 遍历Model类的属性

    iOS~遍历Model类的属性并完善使用Runtime给Model类赋值 一、获取Model的实体属性 1.要想遍...

  • 关于Json转model的一些小想法

    1、json转model的核心设计思想就是使用runtime+递归的思想 runtime的思想: 第一步:取出类里...

  • 利用Runtime动态绑定Model属性

    利用Runtime动态绑定Model属性 大家如果在开发中使用过从网络获取JSON数据,那么一定对model.va...

  • 使用runtime->json转model

    分享一个使用runtime进行json转model的方法, 有很多开源库, 比如YYModel, MJExtens...

  • JSON库

    JSON库将json对象或节点转换成字典、将json数组转换成列表、将json字符串转换成python字符串,tr...

网友评论

    本文标题:iOS --- 使用runtime将JSON转换成Model

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