iOS 封装BaseModel

大家都知道,当我们想要把请求回来的数据转化成model属性,首先是通过这样赋值的:

        for (NSDictionary *dic in jsonDic[@"subjects"]) {    // jsonDic[@"subjects"] 请求回来的数据:
/*
date: 9月4日 - 9月6日
subjects: [ - 
{ - 
box: 9350000
new: false
rank: 1
subject { - 
rating: { + }
genres: [ + ]
collect_count: 14
casts: [ + ]
title: 战争房间
original_title: War Room
subtype: movie
directors: [ + ]
year: 2015
images: { + }
alt: http://movie.douban.com/subject/26591731/
id: 26591731
}
}
*/
 
            NSDictionary *dic2 = dic[@"subject"];

/* 
遍历拿到:
subject { - 
rating: { + }
genres: [ + ]
collect_count: 14
casts: [ + ]
title: 战争房间
original_title: War Room
subtype: movie
directors: [ + ]
year: 2015
images: { + }
alt: http://movie.douban.com/subject/26591731/
id: 26591731
}
*/
            
            FilmModel *model = [[FilmModel alloc] init];
            model.title = dic2[@"title"];     // 这里的赋值都是set方法,所以我只要把这句话封装一下就好了。
            model.year = dic2[@"year"];
            model.collect_count = dic2[@"collect_count"];
            
            [_data addObject:model];
        }



这样在每个类要请求数据的时候每次都要写这么多烦不烦啊! 



实现:从第三个开始看重点看。
//

//  BaseModel.m

//  时空影院

//

//  Created by teather on 15-9-14.

//  Copyright (c) 2015年 黄江桂. All rights reserved.

//


#import "BaseModel.h"

@implementation BaseModel

// 1. 找到JSONKey跟属性的对应关系(属性名和JSONKey相同)
- (NSMutableDictionary *)buildRelationship:(NSDictionary *)json {
    
    // 因为属性和字典的值都是一模一样的,所以在这里只要把他们的value 和model.attribute都对应上就可以了 
    NSMutableDictionary *relationDic = [NSMutableDictionary dictionary];    // key和属性名的映射关系字典
    
    for (NSString *key in [json allKeys]) { // 取出JSON字典里的所有Key
        
        if ([key isEqualToString:@"id"]) {  // 处理id特殊情况

            // [NSStringFromClass([self class])  从字符串中生成一个类名
            [relationDic setObject:[NSStringFromClass([self class]) stringByAppendingString:@"ID"] forKey:@"id"];   // 属性名:类名+ID
        } else {
            // 给对应的key赋值 
            [relationDic setObject:key forKey:key]; // 多数情况下,属性名(object)和JSON数据中的Key(key)名字相同
        }
    }
     // 结束之后就能达到每个value都跟key是一样的。
    return relationDic;
}

// 2. 获取子类的属性名字,找到setter方法
- (SEL)findSetterMethod:(NSString *)propertyName {
    
    // 从字符串生成一个方法(SEL类型)

生成set属性的方法,例如:setPropertyName:(NSString *)propertyName;    NSSelectorFromString// 根据字符串生成一个方法
    return NSSelectorFromString([NSString stringWithFormat:@"set%@:",[[[propertyName substringToIndex:1] uppercaseString] stringByAppendingString:[propertyName substringFromIndex:1]]]);
}


- (void)setValuesForAttributeKeysWithDictionary:(NSDictionary *)keyedValues {    // keyedValues相当于JSON请求回来的字典(数据)
        
    // 1. 获取映射的字典
    NSDictionary *relationDic = [self buildRelationship:keyedValues]; // 等于第一个方法里面的字典
    
    // 2. 找到set方法,给属性赋值相当于现实:model.year = dic2[@"year"];
    for (NSString *key in [relationDic allKeys]) {

       SEL setter = [self findSetterMethod:relationDic[key]];   // 根据属性名找到setter方法
        
        if ([self respondsToSelector:setter]) { // 判断方法是否存在
            
            [self performSelector:setter withObject:keyedValues[key]];  // 给setter方法赋值
        }
    }
}
@end



完成了,下次需要赋值的时候直接掉用:

        for (NSDictionary *dic in jsonDic[@"subjects"]) {
            
            NSDictionary *dic2 = dic[@"subject"];

            FilmModel *model = [[FilmModel alloc] init]; 
            [model setValuesForAttributeKeysWithDictionary:dic2];    // 封装的BaseModel方法给model属性赋值
            [_data addObject:model];
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值