.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