方法一:

//
// 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. wpgcms---字符串截取

    在使用wpgcms做项目的时候,会用到要对描述做字符串截取,具体方法如下: {{ contentInfo.summary|slice(0,75) }}

  2. HDU - 2043密码 水题

    密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  3. 升级 phpstud y中的 mysql 版本

    1.找到你 phpstudy 安装目录,找到 MySQL 文件夹 (我自己的实际目录  D:\pc\phpstudy\MySQL),关掉退出 phpstudy服务,删除 MySQL 文件夹里的文件,如 ...

  4. ora-01017 和oracle database link

    DB link ,创建完了后总是报密码错误.我感到奇怪.明明密码是对的.怎么可能错误呢. SQL> create public database link kk40 connect to khf ...

  5. 19. vue的原理

    vue:原理1 => Object.defineProperty 当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Obj ...

  6. mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    错误情况 解决办法: 首先查看mysql的命令 其次修改root用户的密码 set password for 'root'@'localhost' = password('123456'); 最后退出 ...

  7. python语法_终止循环_break_continue

    break 终止整个循环计算 continue 终止本次循环,continue前的代码执行,continue后的代码不执行,下次循环继续.

  8. Codeforces 1138 - A/B/C/D/E - (Undone)

    链接:https://codeforces.com/contest/1137 A - Skyscrapers 题解:对于每一段 $1$ 和每一段 $2$,统计他们的长度.因此对于相邻的两段长度求较小值 ...

  9. ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ...

  10. SqlHelper2

    public abstract class DBHelper { public static string connectionString = @"DATA SOURCE=.\sqldat ...