- #import <Foundation/Foundation.h>
- @interface BaseModel : NSObject
- //自定义初始化
- - (id)initWithDic:(NSDictionary *)dict;
- //1.将字典中value交给model的属性
- - (void)setShuXingWith:(NSDictionary *)dict;
- //2.将jsonkey作为属性名: 映射关系
- - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic;
- + (id)modelWithDict:(NSDictionary *)dict;
- @end
- #import "BaseModel.h"
- @implementation BaseModel
- + (id)modelWithDict:(NSDictionary *)dict
- {
- return [[self alloc] initWithDic:dict];
- }
- /*
- 将字典中value交给model的属性
- */
- //自定义初始化
- - (id)initWithDic:(NSDictionary *)dict {
- if (self = [super init]) {
- //将字典中value交给model的属性
- [self setShuXingWith:dict];
- }
- return self;
- }
- //1.将字典中value交给model的属性
- - (void)setShuXingWith:(NSDictionary *)dict {
- //已知数据:jsonDic: jsonKey jsonValue
- //可以将jsonKey作为model的属性名
- // jsonKey:model
- //1.取得属性的名字
- //json的key : 属性名
- NSDictionary *mapDic = [self attributeMapDic:dict];
- for (NSString *jsonKey in mapDic) {
- //取得属性名
- NSString *modelArrt = [mapDic objectForKey:jsonKey];
- //取得对应的setter方法
- SEL sel = [self stringToSel:modelArrt];
- //取得json中对象的value
- id value = [dict objectForKey:jsonKey];
- //如果value是null
- if ([value isKindOfClass:[NSNull class]]) {
- value = @"";
- }
- //判断self是否实现了sel
- if ([self respondsToSelector:sel]) {
- [self performSelector:sel withObject:value];
- }
- }
- }
- //生成setter的方法 name age height newId setNewId:
- - (SEL)stringToSel:(NSString *)modelArrt {
- //newId -> Newid
- // [modelArrt capitalizedString];
- //截取属性首字母
- NSString *firstStr = [modelArrt substringToIndex:1];
- //首字母转换成大写
- firstStr = [firstStr uppercaseString];
- //首字母以外的字符串
- NSString *endStr = [modelArrt substringFromIndex:1];
- NSString *sel = [NSString stringWithFormat:@"set%@%@:",firstStr,endStr];
- //将字符串转换成sel
- SEL selector = NSSelectorFromString(sel);
- return selector;
- }
- //2.将jsonkey作为属性名: 映射关系
- - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic {
- NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- for (NSString *jsonKey in jsonDic) {
- [dic setObject:jsonKey forKey:jsonKey];
- }
- return dic;
- }
- 使用:如果模型属性和Key名称属性不一样 需要用下面的方法映射
- //设置映射关系 json key : 模型属性名
- - (NSDictionary *)attributeMapDic:(NSDictionary *)jsonDic {
- // JSONKEY:model属性名
- NSDictionary *dic = @{
- @"created_at":@"createDate",
- @"id":@"weiboId",
- @"text":@"text",
- @"source":@"source",
- @"favorited":@"favorited",
- @"thumbnail_pic":@"thumbnailImage",
- @"bmiddle_pic":@"bmiddlelImage",
- @"original_pic":@"originalImage",
- @"geo":@"geo",
- @"reposts_count":@"repostsCount",
- @"comments_count":@"commentsCount"
- };
- return dic;
- }
- //当模型属性名和字典取值的key不一致的时候
- - (void)setShuXingWith:(NSDictionary *)jsonDic
- {
- [super setShuXingWith:jsonDic];
- self.ID = jsonDic[@"id"];
- }
- 当模型中包含模型属性时
- - (void)setShuXingWith:(NSDictionary *)dict
- {
- [super setShuXingWith:dict];
- // 取出user对应的字典赋值给模型属性
- NSDictionary *userDic = [dict objectForKey:@"user"];
- if (userDic != nil) {
- self.user = [UserModel modelWithDict:userDic];
- }
- // 取出retweeted_status对应的字典赋值给模型属性
- NSDictionary *rDict = [dict objectForKey:@"retweeted_status"];
- if (rDict != nil) {
- self.retweeted_status = [StatusesModel modelWithDict:rDict];
- }
- }