方法一:

//
// 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. geopandas overlay 函数报错问题解决方案

    前言 这篇文章依旧是基于上一篇文章(使用Python实现子区域数据分类统计)而写,此文章中介绍了使用 geopandas 的 overlay 函数对两个 GeoDataFrame 对象取相交或相异的部 ...

  2. express.js graphql express-graphql

    文档 创建应用 const l = console.log; var express = require("express"); var graphqlHTTP = require ...

  3. Python学习之旅(二十)

    Python基础知识(19):面向对象高级编程(Ⅱ) 定制类 形如“__xx__”的变量或函数在Python中是有特殊用途的 1.__str__ 让打印出来的结果更好看 __str__:面向用户:__ ...

  4. PAT甲级1080 Graduate Admission【模拟】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136 题意: 模拟高考志愿录取. 考生根据总 ...

  5. Web开发——HTML基础(高级文本格式 列表/style)

    文档资料参考: 参考:https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Advanced_text_fo ...

  6. AndrewNG Deep learning课程笔记 - CNN

    参考, An Intuitive Explanation of Convolutional Neural Networks http://www.hackcv.com/index.php/archiv ...

  7. windows 创建共享文件夹

    1.右键要共享的文件夹----“共享”------“指定特定用户”,出现如下对话框: 2.选择“Everyone”,点击“共享”按钮,如下: 3.打开“运行“(win+r),输入上图 双反斜杠后面的内 ...

  8. Oracle数据库分组排序

    select row_number() over(partition by oea03 order by oea02 desc) num,oea01,oea02,oea03 from oea_file ...

  9. python list的函数

    1. list.append(obj) 在列表末尾添加新的对象 2. list.count(obj) 统计某个元素在列表中出现的次数 3. list.extend(seq) 在列表末尾一次性追加另一个 ...

  10. Linq动态查询

    public class ExpressionCall { List<Customer> customers = new List<Customer>() { new Cust ...