批量操作RunTime之获取的Dic换成Model

 

方法一:

//
//  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 = 0;
    Ivar *members = class_copyIvarList([infoClass class], &count); //获取属性列表
    for (int i = 0 ; 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(0, 1) withString:@"_iOT"]; //_iOTXXX
    Ivar iotIvar = class_getInstanceVariable([self class], [_iOTName UTF8String]); //IOTBaseModel
    
    [_iOTName deleteCharactersInRange:NSMakeRange(0, 1)]; //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:1];
    if (![firstChar isEqualToString:@"_"] ) {
        [resultStr insertString:@"_" atIndex:0];
    }
    
    return resultStr;
}

- (NSString *)upperPropertyName:(NSString *)className
{
    NSMutableString * key =  [[NSMutableString alloc]initWithString:className];
    NSString *firstChar = [key substringToIndex:1];
    [key replaceCharactersInRange:NSMakeRange(0, 1) 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(0, 1) withString:@"_iOT"]; //_iOTXXX
    
        
        [_iOTName deleteCharactersInRange:NSMakeRange(0, 1)]; //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);
             }
         }
    }
    
    
    
}

 

posted @ 2018-10-25 19:28  尘恍若梦  阅读(279)  评论(0编辑  收藏  举报