model设置属性的封装(存取文件数据,为cell传数据)

来源:互联网 发布:ubuntu 列出所有用户 编辑:程序博客网 时间:2024/05/18 08:54

构建步骤:
1.获取到映射字典
2.找到set方法
//3 赋值

////  BaseModel.m//  WXMovie////  Created by mac on 16/7/22.//  Copyright © 2016年 huang. All rights reserved.//#import "BaseModel.h"@implementation BaseModel- (NSMutableDictionary *) _buildRelationShip:(NSDictionary *)dic {    NSArray *allKeys = [dic allKeys];    NSMutableDictionary *mutableDic  = [NSMutableDictionary dictionary];    for (NSString *key in allKeys) {        if ([key isEqualToString:@"id"]) {            //获取该类的字符串            NSString *className = NSStringFromClass([self class]);            NSString *newString = [NSString stringWithFormat:@"%@Id",className ];            [mutableDic setObject:newString forKey:@"id"];        }else if ([key isEqualToString:@"release"]){            //获取该类的字符串            NSString *className = NSStringFromClass([self class]);            NSString *newString = [NSString stringWithFormat:@"%@Release",className ];            [mutableDic setObject:newString forKey:@"release"];        }        else{            [mutableDic setObject:key forKey:key];        }    }    return mutableDic;}- (SEL) _findSetterMethod: (NSString *) name {//    //1 取首字母  R    NSString *firstLetter = [[name substringToIndex:1] uppercaseString];////    //2 截取剩下的字母  ating//        NSString *lastLetter = [name substringFromIndex:1];    //拼接    setRating:    NSString *setterName = [NSString stringWithFormat:@"set%@%@:",firstLetter,lastLetter];    //4 SEL    SEL setter = NSSelectorFromString(setterName);    return setter;}- (void) setValueForAttributesWithDictionary:(NSDictionary *) keydedValues {    //1.获取到映射字典    NSMutableDictionary *relationDic = [self _buildRelationShip:keydedValues];    //2.找到set方法    NSArray *allKeys = [relationDic allKeys];    for (NSString *key in allKeys) {        NSString *propertyName = [relationDic objectForKey:key];        SEL method = [self _findSetterMethod:propertyName];        //3 赋值        id value = [keydedValues objectForKey:key];        if ([self respondsToSelector:method]) {            [self performSelector:method withObject:value];        }    }   }@end
0 0