首页 > 学院 > 开发设计 > 正文

iOS开发--BaseModel

2019-11-14 18:21:15
字体:
来源:转载
供稿:网友

  软件开发过程中,需要解析各种各样的数据.如最基础的plist文件,从网络下载获取的json数据,以及xml网页数据,还有比较重要的Core Data数据.

  下面我来分享一种快速将json文件中的字典转为模型的方法.虽然很多人在用第三方类库解析json数据,不过将json文件中的字典转为模型内部的实现原理还是需要了解一下的.

  下面是BaseModel.h声明文件,向外界开方了两个方法:

  第7行的方法是实现json文件中的字典转模型;

  第10行方法是防止出现特殊类型的数据无法转为模型而导致程序异常.

 1 BaseModel.h 2  3 #import <Foundation/Foundation.h> 4  5 @interface BaseModel : NSObject 6  7 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary; 8  9 // 将json中的value值交给model的属性(覆写该方法可以将json文件中特殊的数据加到model中)特殊类型如:NSNull类型数据10 - (void)setAttributesWithDictionary:(NSDictionary *)jsonDict;11 12 @end

  

  下面是BaseModel.m实现文件

 

 1 BaseModel.m 2  3 #import "BaseModel.h" 4  5 @implementation BaseModel 6  7 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary 8 { 9     self = [super init];10     11     if (self) {12         // 将jsonDictionary字典的value值交给model的属性值13         [self setAttributesWithDictionary:jsonDictionary];14     }15     return self;16 }17 18 // 将jsonDictionary字典的value值交给model的属性值19 - (void)setAttributesWithDictionary:(NSDictionary *)jsonDictionary20 {21     // 获取映射关系22     NSDictionary *modelDictionary = [self attributesModel:jsonDictionary];23     24     for (NSString *jsonKey in modelDictionary) {25         // 取得属性名26         NSString *modelAttributesNmae = [modelDictionary objectForKey:jsonKey];27         28         // 取得Value值29         id jsonValue = [jsonDictionary objectForKey:jsonKey];30         31         // 获取属性的setter方法32         SEL setterSEL = [self stringToSEL:modelAttributesNmae];33         34         // 如果Value值为NULL,则赋值为""(什么也没有)35         if ([jsonValue isKindOfClass:[NSNull class]]) {36             jsonValue = @"";37         }38         39         // Warning:PerformSelector may cause a leak because its selector is unknown(PerformSelector可能导致内存泄漏,因为它选择器是未知的)40         if ([self respondsToSelector:setterSEL]) {41             [self performSelector:setterSEL withObject:jsonValue];42         }43         44     }45 }46 47 // 将字典中的key和model的属性名映射48 - (NSDictionary *)attributesModel:(NSDictionary *)jsonDictionary49 {50     NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];51     52     // 属性名和jsonDictionary的key一样53     for (NSString *jsonKey in jsonDictionary) {54         [dict setValue:jsonKey forKey:jsonKey];55     }56     return dict;57 }58 59 // 获取属性的setter方法60 - (SEL)stringToSEL:(NSString *)modelAttributes61 {62     // 截取属性名的首字母63     NSString *firstString = [modelAttributes substringToIndex:1];64     65     // 首字母大写66     firstString = [firstString uppercaseString];67     68     // 截取除首字母以外的其它属性名内容69     NSString *endString = [modelAttributes substringFromIndex:1];70     71     // 拼接setter方法名72     NSString *selString = [NSString stringWithFormat:@"set%@%@:",firstString, endString];73     74     // 将字符串转化为方法75     SEL selector = NSSelectorFromString(selString);76     77     return selector;78 }79 80 @end

 

  其中BaseModel.m实现文件的第41行会报一个警告,警告的内容上面已经给出了.

  到此为止,一个将json文件中的字典转为模型的类已经实现了.

  BaseModel的使用大家可以自己测试一下,在这里就不在演示了.

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表