一、介绍

模型转字典,字典转模型,这是开发中最基本的功能。系统类中提供了一个setValuesForKeysWithDictionary方法来实现字典转模型,至于模型转字典,这个就需要使用runtime来实现了。其实字典和模型的互转可以完全使用运行时runtime来实现。典型的第三方有MJExtension和YYModel。现在我就来借助runtime的思想来进行大致的实现。

二、思路

  • 字典转模型

    • 遍历key
    • objc_msgSend对key发送消息
  • 模型转字典
    • class_copyPropertyList获取属性列表properties
    • 遍历properties,获取属性名name
    • objc_msgSend对name发送消息,设置value
    • 存入dic并返回

三、示例

person.h

//
// Person.h
// 运行时
//
// Created by 夏远全 on 2019/10/11.
// Copyright © 2019 北京华樾教育科技有限公司. All rights reserved.
// #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Person : NSObject
@property (nonatomic, copy) NSString *age;
@property (nonatomic, copy) NSString *name; /**
字典转模型
@param dic 字典
@return 模型
*/
-(instancetype)initWithDictionary:(NSDictionary *)dic; /**
模型转字典
@return 字典
*/
-(NSDictionary *)convertModelToDictionary; @end NS_ASSUME_NONNULL_END

person.m

//
// Person.m
// 运行时
//
// Created by 夏远全 on 2019/10/11.
// Copyright © 2019 北京华樾教育科技有限公司. All rights reserved.
// #import "Person.h"
#import <objc/message.h> @implementation Person -(instancetype)initWithDictionary:(NSDictionary *)dic{ self = [super init];
if (self) {
for (NSString *key in dic.allKeys) {
//创建一个set选择器
NSString *methodName = [NSString stringWithFormat:@"set%@:",key.capitalizedString];
SEL selector = NSSelectorFromString(methodName); //类型异常处理
id value = dic[key];
if([value isKindOfClass:[NSNull class]]) continue; //发送消息
if (!selector) continue;
((void (*)(id, SEL, id))objc_msgSend)(self, selector, value);
}
}
return self;
} -(NSDictionary *)convertModelToDictionary { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (int i=; i<count; i++) {
objc_property_t property = properties[i]; //属性名称
const char * name = property_getName(property);
NSString *propertyName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding]; //创建一个get选择器
SEL selector = NSSelectorFromString(propertyName); //发送消息
if (!selector) continue;
id value = ((id (*)(id, SEL))objc_msgSend)(self, selector); //空与类型异常处理
if(!value || [value isKindOfClass:[NSNull class]]) continue; //值存储
[dic setValue:value forKey:propertyName];
} //copy创建的需要释放
free(properties); return dic;
} @end

四、测试

1、dic -> model

-(void)test_objc_DicToModel {

    /// 这里主要提供一个思想,全部用字符串作为字段。其他类型:对象类型/NSNumber/int/float...,需要自己再去特殊处理
NSDictionary *dic = @{@"age":@"",@"name":@"张三"};
Person *person = [[Person alloc] initWithDictionary:dic];
NSLog(@"age=%@,name=%@",person.age, person.name);
}
-- ::47.366662+ 运行时[:] age=,name=张三

2、model -> dic

-(void)test_objc_ModelToDic {

    NSDictionary *dic = @{@"age":@"",@"name":@"张三"};
Person *person = [[Person alloc] initWithDictionary:dic];
NSDictionary *dic2 = [person convertModelToDictionary];
NSLog(@"%@",dic2);
}
-- ::47.367024+ 运行时[:] {
age = ;
name = "\U5f20\U4e09";
}

使用Runtime的objc_msgSend实现模型和字典的互转的更多相关文章

  1. KVC简介 -字典转模型,模型转字典

    // 下面两个方法.都属于 KVC 的方法 // KVC 是 cocoa 的大招.间接给对象属性设置数值 // 程序运行过程中,动态给对象属性设置数值.不关心 .h 中是怎样定义的 //      仅 ...

  2. 利用runTime,实现以模型为主的字典转模型(注意与KVC的区别)

    将字典转化为模型,面向模型开发,是在开发中最为常用的功能.利用KVC可以将字典转换为模型,但是前提有三个约束,一个是必须保证模型的属性个数大于等于字典个数,二是属性名称与字典的key必须相同,三是对于 ...

  3. IOS中用模型取代字典的好处

    使用字典的坏处 一般情况下,设置数据和取出数据都是用“字符串类型的key”,编写这些key时,编译器不会有任何友情提示,需要手敲 dict[@“name”]=@“Kevin”; NSString *n ...

  4. flask学习(十):模板中访问模型和字典的属性

    访问模型中的属性或者是字典,可以通过{{params.property}}的形式,或者是使用{{params['age']}}这样的形式

  5. python字符串字典列表互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  6. ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型

    一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...

  7. iOS 字典转模型Model

    基本原理 利用 runtime 原理,获取模型中所有实例变量列表,根据实例变量以此获取模型中成员变量的名称和属性类型,区分Foundation和自定义属性,需要对NSDictionary和NSArra ...

  8. JsonString,字典,模型之间相互转换

    NSData转字符串 [NSString alloc] initWithData: encoding:] 模型转字典 attInfo.keyValues 字典转模型 ZTEOutputInfo *ou ...

  9. iOS开发UI篇—字典转模型

    iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...

随机推荐

  1. SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ID 2340986.1)

    SLES 12: Database Startup Error with ORA-27300 ORA-27301 ORA-27303 While Starting using Srvctl (Doc ...

  2. Windows 10 神州网信版

    一.版本介绍:官网链接:http://document.cmgos.com/release_notes/release_notes_V0_H 下载链接:请自行百度Windows 10 神州网信政府版( ...

  3. [译]Vulkan教程(14)图形管道基础之固定功能

    [译]Vulkan教程(14)图形管道基础之固定功能 Fixed functions 固定功能 The older graphics APIs provided default state for m ...

  4. 【ES6学习笔记之】Object.assign()

    基本用法 Object.assign方法用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target). const target = { a: 1 }; const sou ...

  5. Java:程序不过是几行代码的集合

    程序不过是几行代码的集合.就像下面这样: public class Test { public static void main(String[] args) { System.out.println ...

  6. Selenium(十二):操作Cookie、调用JavaScript、HTML5的视频播放

    1. 操作Cookie 有时候我们想要验证浏览器中cookie是否正确,因为基于真实cookie的测试是无法通过白盒和集成测试的.WebDriver提供了操作Cookie的相关方法,可以读取.添加和删 ...

  7. vue-cli 引用elementUI打包后文件过大

    解决方案:使用externals引用第三方资源,防止element资源被打包到自己项目中,(总共修改3个页面index.html.webpack.base.conf.js.main.js) 1.修改i ...

  8. JS While

    JS While 只要指定条件为 true,循环就可以一直执行代码. while 循环 While 循环会在指定条件为真时循环执行代码块. 语法 while (条件) { 需要执行的代码 } whil ...

  9. presentViewController底部弹框适配ipad

    //适配ipad if ([alert respondsToSelector:@selector(popoverPresentationController)]) { alert.popoverPre ...

  10. [转载] Java 遍历 Map 的 5 种方式

    目录 1 通过 keySet() 或 values() 方法遍历 2 通过 keySet 的 get(key) 获取值 3 通过 entrySet 遍历 4 通过迭代器 Iterator 遍历 5 通 ...