方法一:

//
// AlinkDeviceInfo.m
////
// Created by Vivien on 2018/10/12.
// Copyright © 2018年 . All rights reserved.
// #import "AlinkDeviceInfo.h"
#import <objc/runtime.h>
@implementation AlinkDeviceInfo - (void)setModels
{
//获取当前类
id infoClass = [self class];
unsigned int count = ;
Ivar *members = class_copyIvarList([infoClass class], &count); //获取属性列表
for (int i = ; i < count; i++) { //遍历属性列表
Ivar var = members[i];
const char *memberType = ivar_getTypeEncoding(var); //获取变量类型
NSString *typeStr = [NSString stringWithCString:memberType encoding:NSUTF8StringEncoding];
if ([typeStr isEqualToString:@"@\"NSDictionary\""]) { //判断类型是否为字典
const char *memberName = ivar_getName(var); //获取变量名称
[self setModelWithDicName:[NSString stringWithCString:memberName encoding:NSUTF8StringEncoding] channel:IOTBaseModelValueChangedChannel_Get];
}
}
} /**
给iOT对象赋值
将NSDictionary对象解析并赋值给对应的IOTBaseModel
eg:
NSString *jsonStr = _Speed[@"value"];
NSDictionary *modelDict = [self dictionaryWithJsonString:jsonStr];
_iOTSpeed = [IOTSpeed mj_objectWithKeyValues:modelDict]; */
- (void)setModelWithDicName:(NSString *)name channel:(IOTBaseModelValueChangedChannel )channel //channel:IOTBaseModelValueChangedChannel_Push
{
NSString *dicName = [self addLineToName:name]; //_Dic
NSMutableString *_iOTName = [[NSMutableString alloc]initWithString:dicName];
[_iOTName replaceCharactersInRange:NSMakeRange(, ) withString:@"_iOT"]; //_iOTXXX
Ivar iotIvar = class_getInstanceVariable([self class], [_iOTName UTF8String]); //IOTBaseModel [_iOTName deleteCharactersInRange:NSMakeRange(, )]; //iOTName SEL iotGetSEL = NSSelectorFromString(_iOTName);
if ([self respondsToSelector:iotGetSEL]) { //存在对应的iOT对象 Ivar ivar = class_getInstanceVariable([self class], [dicName UTF8String]); //字典对象 NSDictionary *tempDic = object_getIvar(self, ivar);
if(tempDic){
NSString *jsonStr = tempDic[@"value"];
NSDictionary *modelDict = [self dictionaryWithJsonString:jsonStr]; NSMutableString *modelName = [[NSMutableString alloc]initWithString:[self upperPropertyName:_iOTName]];
Class modelClass = NSClassFromString(modelName);
NSLog(@"tempDic::%@,modelDict:%@,iOTName:%@,modelName:%@", tempDic,modelDict,_iOTName,modelName); id tempObj = [modelClass mj_objectWithKeyValues:modelDict];
object_setIvar(self, iotIvar, tempObj); if (channel == IOTBaseModelValueChangedChannel_Push ) {
IOTBaseModel *model = object_getIvar(self, iotIvar);
[SharedGLBRobotResponseHandler onRobotResponse:CURRENT_ACTIVE_ROBOT value:model key:[model lowerPropertyName:NSStringFromClass(model.class)] channel:IOTBaseModelValueChangedChannel_Push];
} }
}
} - (NSString *)addLineToName:(NSString *)name
{
NSMutableString *resultStr = [[NSMutableString alloc]initWithString:name];
NSString *firstChar = [resultStr substringToIndex:];
if (![firstChar isEqualToString:@"_"] ) {
[resultStr insertString:@"_" atIndex:];
} return resultStr;
} - (NSString *)upperPropertyName:(NSString *)className
{
NSMutableString * key = [[NSMutableString alloc]initWithString:className];
NSString *firstChar = [key substringToIndex:];
[key replaceCharactersInRange:NSMakeRange(, ) withString:[firstChar uppercaseString]];
return key;
}
//解析
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
if (jsonString == nil) {
return nil;
} NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err)
{
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
} -(id)valueForUndefinedKey:(NSString *)key
{
NSLog(@"你有get方法没实现,key:%@",key);
return nil;
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"你有set方法没实现,key:%@",key);
} @end

这样子有个问题:

 Ivar ivar = class_getInstanceVariable([self class], [dicName UTF8String]);  //字典对象

获取不到Category中的属性
- (void)setModels
{ NSArray *keys = [self getProperties];
//(2)根据类型给属性赋值
for (NSString * key in keys) {
NSMutableString *_iOTName = [[NSMutableString alloc]initWithString:[self addLineToName:key]];
[_iOTName replaceCharactersInRange:NSMakeRange(, ) withString:@"_iOT"]; //_iOTXXX [_iOTName deleteCharactersInRange:NSMakeRange(, )]; //iOTName SEL iotGetSEL = NSSelectorFromString(_iOTName);
if ([CURRENT_ACTIVE_ROBOT respondsToSelector:iotGetSEL]) { //存在对应的iOT对象
Ivar ivar = class_getInstanceVariable([self class], [[self addLineToName:key] UTF8String]); //字典对象
NSDictionary *tempDic = object_getIvar(self, ivar);
if(tempDic){
NSString *jsonStr = tempDic[@"value"];
NSDictionary *modelDict = [self dictionaryWithJsonString:jsonStr];
NSMutableString *modelName = [[NSMutableString alloc]initWithString:[self upperPropertyName:_iOTName]];
Class modelClass = NSClassFromString(modelName);
id tempObj = [modelClass mj_objectWithKeyValues:modelDict];
[CURRENT_ACTIVE_ROBOT setValue:tempObj forKey:_iOTName];
NSLog(@"EcoAllRobot:%ld,%ld",CURRENT_ACTIVE_ROBOT.iOTSpeed.speed,CURRENT_ACTIVE_ROBOT.iOTBreakPoint.enable);
NSLog(@"tempDic::%@,modelDict:%@,iOTName:%@,modelName:%@", tempDic,modelDict,_iOTName,modelName);
}
}
} }

批量操作RunTime之获取的Dic换成Model的更多相关文章

  1. JS代码获取当前日期时支持IE,不兼容FF和chrome,解决这个问题,我们需要把获取时间的getYear()函数换成getFullYear()

    以前在页面中获得当前时间的方法如下: function SelectTodayClient() { var d = new Date(); var taday = d.getYear() + &quo ...

  2. php 接收二进制流转换成图片

    php 接收二进制流转换成图片,图片类imageUpload.php如下: <?php /** * 图片类 * @author http://blog.csdn.net/haiqiao_2010 ...

  3. SQL Where in (1,2,3,4) 换成字段一列的值

    ) ; , ) ) FROM r_resource WHERE id IN ( @resource) 换成 ) : , ) ) FROM r_resource )) SELECT cid,id FRO ...

  4. 将几何画板x轴坐标值换成弧度制的方法

    大家在学习数学的过程中,都会遇到函数,这个时候大家都会遇到函数图像.对于函数图像我们一般都会通过先建立坐标系,然后让图像呈现在坐标系中.函数的种类有很多,三角函数就是其中之一,在绘制三角函数图像时,我 ...

  5. Win8换成Win7系统问题小结(修改主板BIOS方法)

    问题描述: 笔记本电脑W8系统使用不习惯,想要换成W7系统,但不管是用光盘安装亦或是用U盘安装,在设置系统启动项的时候,选择从光盘启动或从U盘启动,但是回车点了之后没反应. 下面就说说问题的原因及解决 ...

  6. php 把数字1-1亿换成汉字表述,例如 150 转成 一百五十

    /* 额,重新修改了下.现在估计没什么问题了.... */ 直接上实例 写到 千亿上了. /** * @author ja颂 * 把数字1-1亿换成汉字表述,如:123->一百二十三 * @pa ...

  7. 【代码笔记】iOS-把<br!>换成\n

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  8. iOS: 上传App到AppStore,由于Xcode上传太慢,换成Application Loader上传,速度秒传

    一.遇到的遭遇 在之前的项目开发中,本人有点固执,一直采用xcode打包后再上传,结果可想而知: (1)要么上传时速度慢的跟蜗牛似的,等的我心力交瘁(不排除网络不给力的原因,公司这个吊问题快把我气疯了 ...

  9. svn的差异查看器和合并工具换成BCompare.exe

    svn的差异查看器和合并工具换成BCompare.exe

随机推荐

  1. 使用redis接管cookie

    class RedisCookie { // 默认配置名称(使用load_config加载) private $_default_config_path = 'package/cache/redis_ ...

  2. 山东13年省赛 Aliceand Bob

    Problem F: Alice and Bob Description Alice and Bob like playing games very much.Today, they introduc ...

  3. 【转】前端框架天下三分:Angular React 和 Vue的比较

    前端框架天下三分:Angular React 和 Vue的比较 原文链接:http://blog.csdn.net/haoshidai/article/details/52346865 前端这几年的技 ...

  4. python练习题-day12

    用列表推导式做下列小题 (1) 过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母 lst1=["admhdja","aksaudj","fh&q ...

  5. 消息摘要、哈希(hash)、加盐

    hashlib 算法介绍: python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度 ...

  6. PHP 二位数组按照下标排序

    1.排序得内容 array(6) { [0] => array(12) { [0] => string(3) "160" [1] => string(2) &qu ...

  7. C++的默认构造函数

    待看文章:C++ 合成默认构造函数的真相 默认构造函数指不带参数或者所有参数都有缺省值的构造函数!!! 类的默认构造函数可以使得在实例化该类的对象时不用提供参数,但是类也可以不含默认构造函数,这样在实 ...

  8. JUnit单元测试代码

    package com.storage.test; import org.junit.Before; import org.junit.Test; import org.springframework ...

  9. 分布式异步任务Celery

    -A代表APP celery -A tasks worker --loglevel=info -n nodemaster -------------- celery@nodemaster v4.1.0 ...

  10. #WEB安全基础 : HTTP协议 | 0x6 初识HTTP报文

    欢迎来到HTTP最精彩的部分 请注意:应用HTTP协议时,必定有一方担任客户端,另一方担任服务器 客户端向服务器发出请求,服务器向客户端返回响应 下面是一个请求与相应的例子: 请求: GET /ind ...