zoukankan      html  css  js  c++  java
  • 第二十二篇、服务器返回的数据转成模型

    .h

    #import <Foundation/Foundation.h>
    @protocol BaseModelDelegate<NSObject>
    
    @optional
    
    +(id)input:(id)inputValue;
    
    @end
    @interface BaseModel : NSObject<BaseModelDelegate>//BaseModel遵守协议
    
    
    -(NSDictionary *)speciaKey;
    
    -(NSDictionary *)speciaModel;
    
    
    
    
    @end

    .m

    #import "BaseModel.h"
    
    @implementation BaseModel
    
    -(NSDictionary *)speciaKey{
    
        return nil;
    }
    -(NSDictionary *)speciaModel{
        
        return nil;
    }
    -(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    }
    -(id)valueForUndefinedKey:(NSString *)key
    {
    return @"2";
    }
    +(id)input:(id)inputValue
    {
        id Obj=inputValue;
        if ([Obj isKindOfClass:[NSArray class]]) {
           // NSLog(@"aaa");
            Obj= [self inputArr:inputValue];//NSLog(@"this base model arr") ;
            return Obj;
        }
        if ([Obj isKindOfClass:[NSDictionary class]]) {
           // NSLog(@"bbb");
            Obj=[self inputDic:inputValue];//NSLog(@"this base model dic");
            return Obj;
        }
        
        return Obj;
    }
    
    +(NSArray*)inputArr:(id)inputArr
    {
        NSMutableArray *NewArr=[NSMutableArray new];
        for (id inputObj in inputArr) {
           // NSLog(@"跟踪1");
            if ([inputObj isKindOfClass:[NSDictionary class]]) {
                id Output=[self inputDic:inputObj];
                [NewArr addObject:Output];//NSLog(@"跟踪2");
            }
            if ([inputObj isKindOfClass:[NSArray class]]) {
                
                id Output=[self inputArr:inputObj];
                [NewArr addObject:Output];//NSLog(@"跟踪2");
            }
        }
        return NewArr;
    }
    +(id)inputDic:(id)inputDic
    {
        
        id model=[self new];
      // NSLog(@"I M dic");
        NSDictionary *speciaKey=[model speciaKey];
        NSDictionary *speciaModel=[model speciaModel];
        
        if ([inputDic isKindOfClass:[NSDictionary class]]) {
            
            [inputDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                if ([speciaKey objectForKey:key]) {
                    key=speciaKey[key];//调包
                    
                }
                if ([speciaModel objectForKey:key]) {
                    // NSLog(@"spModel=%@,key=%@,self=%@",speciaModel,key,self);
                    Class class=NSClassFromString(speciaModel[key]);
                    
                    //  NSLog(@"class=%@,obj=%@,,,%@",class,class,speciaModel[key]);
                    obj=[class input:obj];
                    
                    //  NSLog(@"OBJ=%@",obj);
                }
                
                if ((![obj isKindOfClass:[NSNull class]])&&obj!=nil){
                    if ([obj isKindOfClass:[NSArray class]]) {
                        NSArray *a=[NSArray arrayWithArray:obj];
                        if (a.count==0) {
                            [model setValue:@"1" forKey:@"error"];
                            //            NSLog(@"进来?");
                        } else {
                            if ([obj isKindOfClass:[NSString class]]) {
                                NSString *objj=[obj stringByRemovingPercentEncoding];
                                [model setValue:objj forKey:key];
                        // NSLog(@"obj=%@---key=%@----class=%@",objj,key,[objj class]);
                            }else{
                                [model setValue:obj forKey:key];
                            }
                        }
                    }else{
                        //    NSLog(@"%@dad",inputDic[key]);
                        if ([obj isKindOfClass:[NSString class]]) {
                           NSString *objj=[obj stringByRemovingPercentEncoding];
                            [model setValue:objj forKey:key];
                          //  [model setValue:object_getClass(objj) forKey:key];
                            
                            
                   //    NSLog(@"obj=%@---key=%@----class=%@",objj,key,[objj class]);
                        }else{
                        [model setValue:obj forKey:key];
                        }
                        
                        
                        
                        if (![[model valueForKey:@"error"] isEqualToString:@"1"]) {
                            [model setValue:@"0" forKey:@"error"];
                        }
                        
                        [model setValue:@"0" forKey:@"error2"];
                        
                      // NSLog(@"key=%@,value=%@,model=%@",key,obj,model);
                    }
                }else {
                //   NSLog(@"44444444key=%@,value=%@,model=%@",key,obj,model);
                    
                    [model setValue:@"1" forKey:@"error"];}
                
                
            }];
        }
       
        
      //  NSLog(@"model=%@",model);
        
        
        return model;
    }
    
    
    
    
    @end


    使用:

    .h

    #import "BaseModel.h"
    
    @interface ActivityPKListModel : BaseModel
    pron cliche_code;
    pron ret;
    pros NSMutableArray *result;
    pron access_token;
    @end
    @interface  ActivityPKListListModel:BaseModel
    
    // 说明: @property (nonatomic,strong) == pron
    pron mkName; pron yzName; pron mkColor; pron yzColor; pron mkLevel; pron sportId; pron creatTime; pron status; pron sportFmt; pron levelFmt; pron orderwarId; pron venueName; pron agImg; pron mkImg; pron startTime; pron venueAddr; pron groundName; pron mk_war_count; pron yz_war_count; pron end_time; pron mk_orderwar_count; pron yz_orderwar_count;
    @end

    .m

    #import "ActivityPKListModel.h"
    
    @implementation ActivityPKListModel
    -(NSDictionary *)speciaModel
    {
        return @{@"result":@"ActivityPKListListModel"};
    }
    @end
    @implementation ActivityPKListListModel
    
    -(NSDictionary *)speciaKey
    {
        return @{@"mk_name":@"mkName",@"yz_name":@"yzName",@"mk_color":@"mkColor",@"yz_color":@"yzColor",@"mk_level":@"mkLevel",@"sport_id":@"sportId",@"creat_time":@"creatTime",@"sport_fmt":@"sportFmt",@"level_fmt":@"levelFmt",@"orderwar_id":@"orderwarId",@"venue_name":@"venueName",@"ag_img":@"agImg",@"mk_img":@"mkImg",@"start_time":@"startTime",@"venue_addr":@"venueAddr",@"ground_name":@"groundName"};
        
    }
    
    @end
  • 相关阅读:
    图文详解QT布局管理器
    osg中放大缩小模型
    osgearth中XML文档读取报错
    中国河南省洛阳市嵩县黄庄乡红堂村大树芽组
    GIS数据下载整合
    四面体剖分相关开源软件列表
    在你的QT工程中使用ui文件
    对osgAnimation例子的注释的注释
    [debug]调试Release版本应用程序
    链表面试题总结
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5818379.html
Copyright © 2011-2022 走看看