方法一:

//
// 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. python开发工具

    要用到爬虫,网上推荐crapy,自己在pycharm上没有安装成功,于是使用anaconda,但是在gui界面安装crapy总是失败,且没有提示信息.于是使用命令行的方式,提示PermissionEr ...

  2. react useRef()函数

    "ref"对象是一个通用容器,其current属性是可变的 保存dom function Test() { const t = useRef(null); useEffect(() ...

  3. 最新版的Chrome不能设置网页编码怎么解?

    添加一个Google插件https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfilgdlm ...

  4. 搭建svn服务器的坑

    第一次搭建,能启动,就是连接不了 原因:修改配置文件(所有文字前面不能有空格,等号必须空格)

  5. python全栈开发 * 13知识点汇总 * 180619

    13 迭代器和⽣成器一.迭代器 1.以通过dir函数来查看类中定义好的所有⽅法 2.__iter__ 用来获取当前对象的迭代器 3.__next__ 获取可迭代对象的元素s="我爱吃火锅&q ...

  6. Alpine Linux常用命令

    一:Alpine Linux开启SSH远程登陆 1.简介: 最重要的一个服务了,远程登陆需要用它,文件传输需要用它,必备功能.不管你是在实体机上跑,虚拟机上跑,docker里面跑,这个都是必须的. 2 ...

  7. SpringMVC访问静态资源的三种方式

    如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题.如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg ...

  8. Luogu 1068 - 分数线划定 - [快速排序]

    题目链接:https://www.luogu.org/problemnew/show/P1068 题目描述世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对所有报名的选手 ...

  9. php 延迟静态绑定: static关键字

    abstract class DomainObject { public static function create() { return new self(); } } class User ex ...

  10. Git忽略提交规则 - .gitignore配置运维总结

    在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...