BaseMode 数据的封装成对象

[objc]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface BaseModel : NSObject  
  4.   
  5. //自定义初始化  
  6. - (id)initWithDic:(NSDictionary *)dict;  
  7.   
  8. //1.将字典中value交给model的属性  
  9. - (void)setShuXingWith:(NSDictionary *)dict;  
  10.   
  11. //2.将jsonkey作为属性名:  映射关系  
  12. - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic;  
  13.   
  14.   
  15. + (id)modelWithDict:(NSDictionary *)dict;  
  16.   
  17. @end  
  18.   
  19.   
  20. #import "BaseModel.h"  
  21.   
  22. @implementation BaseModel  
  23.   
  24. + (id)modelWithDict:(NSDictionary *)dict  
  25. {  
  26.     return [[self alloc] initWithDic:dict];  
  27. }  
  28.   
  29. /* 
  30.  将字典中value交给model的属性 
  31.  */  
  32.   
  33. //自定义初始化  
  34. - (id)initWithDic:(NSDictionary *)dict {  
  35.       
  36.     if (self = [super init]) {  
  37.           
  38.         //将字典中value交给model的属性  
  39.         [self setShuXingWith:dict];  
  40.           
  41.     }  
  42.       
  43.     return self;  
  44. }  
  45.   
  46. //1.将字典中value交给model的属性  
  47. - (void)setShuXingWith:(NSDictionary *)dict {  
  48.   
  49.     //已知数据:jsonDic:   jsonKey   jsonValue  
  50.       
  51.     //可以将jsonKey作为model的属性名  
  52.     //  jsonKey:model  
  53.   
  54.     //1.取得属性的名字  
  55.     //json的key : 属性名  
  56.     NSDictionary *mapDic = [self attributeMapDic:dict];  
  57.     for (NSString *jsonKey in mapDic) {  
  58.           
  59.         //取得属性名  
  60.         NSString *modelArrt = [mapDic objectForKey:jsonKey];  
  61.           
  62.         //取得对应的setter方法  
  63.         SEL sel = [self stringToSel:modelArrt];  
  64.           
  65.         //取得json中对象的value  
  66.         id value = [dict objectForKey:jsonKey];  
  67.          
  68.         //如果value是null  
  69.         if ([value isKindOfClass:[NSNull class]]) {  
  70.             value = @"";  
  71.         }  
  72.           
  73.         //判断self是否实现了sel  
  74.         if ([self respondsToSelector:sel]) {  
  75.               
  76.             [self performSelector:sel withObject:value];  
  77.               
  78.         }  
  79.           
  80.     }  
  81.       
  82.       
  83.       
  84. }  
  85.   
  86.   
  87. //生成setter的方法  name  age  height  newId   setNewId:  
  88. - (SEL)stringToSel:(NSString *)modelArrt {  
  89.     //newId  ->  Newid  
  90. //    [modelArrt capitalizedString];  
  91.       
  92.     //截取属性首字母  
  93.     NSString *firstStr = [modelArrt substringToIndex:1];  
  94.     //首字母转换成大写  
  95.     firstStr = [firstStr uppercaseString];  
  96.       
  97.     //首字母以外的字符串  
  98.     NSString *endStr = [modelArrt substringFromIndex:1];  
  99.       
  100.     NSString *sel = [NSString stringWithFormat:@"set%@%@:",firstStr,endStr];  
  101.     //将字符串转换成sel  
  102.     SEL selector = NSSelectorFromString(sel);  
  103.       
  104.     return selector;  
  105.       
  106. }  
  107.   
  108. //2.将jsonkey作为属性名:  映射关系  
  109. - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic {  
  110.   
  111.     NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];  
  112.       
  113.     for (NSString *jsonKey in jsonDic) {  
  114.         [dic setObject:jsonKey forKey:jsonKey];  
  115.     }  
  116.    
  117.     return dic;  
  118. }  
[objc]  view plain copy
  1. 使用:如果模型属性和Key名称属性不一样  需要用下面的方法映射  
[objc]  view plain copy
  1. //设置映射关系 json key : 模型属性名  
  2. - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic {  
  3.       
  4.     //  JSONKEY:model属性名  
  5.     NSDictionary *dic = @{  
  6.                           @"created_at":@"createDate",  
  7.                           @"id":@"weiboId",  
  8.                           @"text":@"text",  
  9.                           @"source":@"source",  
  10.                           @"favorited":@"favorited",  
  11.                           @"thumbnail_pic":@"thumbnailImage",  
  12.                           @"bmiddle_pic":@"bmiddlelImage",  
  13.                           @"original_pic":@"originalImage",  
  14.                           @"geo":@"geo",  
  15.                           @"reposts_count":@"repostsCount",  
  16.                           @"comments_count":@"commentsCount"  
  17.                           };  
  18.     return dic;  
  19. }  


 
[objc]  view plain copy
  1. //当模型属性名和字典取值的key不一致的时候  
[objc]  view plain copy
  1. - (void)setShuXingWith:(NSDictionary *)jsonDic  
  2. {  
  3.     [super setShuXingWith:jsonDic];  
  4.       
  5.     self.ID = jsonDic[@"id"];  
  6. }  


 
[objc]  view plain copy
  1. 当模型中包含模型属性时  
[objc]  view plain copy
  1. - (void)setShuXingWith:(NSDictionary *)dict  
  2. {  
  3.     [super setShuXingWith:dict];  
  4.       
  5. //    取出user对应的字典赋值给模型属性  
  6.    NSDictionary *userDic = [dict objectForKey:@"user"];  
  7.     if (userDic != nil) {  
  8.         self.user = [UserModel modelWithDict:userDic];  
  9.     }  
  10.       
  11. //    取出retweeted_status对应的字典赋值给模型属性  
  12.     NSDictionary *rDict = [dict objectForKey:@"retweeted_status"];  
  13.     if (rDict != nil) {  
  14.         self.retweeted_status = [StatusesModel modelWithDict:rDict];  
  15.     }  
  16.       
  17.       
  18. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值