ios 反射,MVC之model

mvc模式大家都很清楚了!最大的好处就是条理清晰一点,view和数据分开,在viewController中只要处理view和数据的逻辑就好了,甚至于逻辑都在model中处理,嗯想说什么呢,过去两个礼拜吧,都在做一件很挫的事情:model都是这样子写的:

@interface SPExangeModel : NSObject
/*!
 *  模型属性
 */
@property (nonatomic, copy) NSString * city;
@property (nonatomic, copy) NSString * content;
@property (nonatomic, copy) NSString * create_time;
@property (nonatomic, copy) NSString * credits;
@property (nonatomic, copy) NSString * eid;
@property (nonatomic, copy) NSString * exchange_time;
@property (nonatomic, copy) NSString * exchanged;
@property (nonatomic, copy) NSString * _id;
@property (nonatomic, copy) NSString * image;
@property (nonatomic, copy) NSString * key;
@property (nonatomic, copy) NSString * num;
@property (nonatomic, copy) NSString * status;
@property (nonatomic, copy) NSString * surplus;
@property (nonatomic, copy) NSString * tag;
@property (nonatomic, copy) NSString * title;

@end

 然后不遗余力的再实现文件中这么写:

@implementation SPExangeModel

- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self)
    {
        self.city      = SPFormatstring(dictionary[@"city"]);
        self.content   = SPFormatstring(dictionary[@"content"]);
        self.create_time = dictionary[@"create_time"];
        self.credits   = SPFormatstring(dictionary[@"credits"]);
        self.eid       = SPFormatstring(dictionary[@"eid"]);
        self.exchange_time = dictionary[@"exchange_time"];
        self.exchanged = SPFormatstring(dictionary[@"exchanged"]);
        self._id       = SPFormatstring(dictionary[@"id"]);
        self.image     = dictionary[@"image"];
        self.key       = dictionary[@"key"];
        self.num       = dictionary[@"num"];
        self.status    = SPFormatstring(dictionary[@"status"]);
        self.surplus   = SPFormatstring(dictionary[@"surplus"]);
        self.tag       = SPFormatstring(dictionary[@"tag"]);
        self.title     = dictionary[@"title"];
    }
    return self;
}
@end

当然,一个类的话无所谓了,可是当道后来,model增加到了几十个之后,嗯,我自己崩溃了,这真的是在搬砖,翻阅了一下runtime头文件,比较有意思,恰好熟悉C#,因为C#中java中都有反射这种说法,所以处理起来就容易很多,嗯ios中也是有的,下面看看改进版本的

model基类:

 

#import <Foundation/Foundation.h>

@interface BaseModel : NSObject
/*!
 *  使用字典初始化
 *
 *  @param dictionary 字典
 *
 *  @return 结果
 */
- (id)initWithDictionary:(NSDictionary *)dictionary;
//获取字典形式:即属性和值对应起来的字典
- (NSDictionary *)toDictionary;

@end

 

#import "BaseModel.h"
#import <objc/runtime.h>

@interface BaseModel(BS)
/*!
 *  获取属性列表
 *
 *  @return 属性列表
 */
- (NSArray *)getPropertys;
@end

@implementation BaseModel

- (id)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self)
    {
        //自己属性列表
        NSArray *keys = [self getPropertys];
        //字典属性列表
        NSArray *dicKeys = [dictionary allKeys];
        
        __weak typeof(self) weakSelf = self;
        [dicKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([keys containsObject:obj])
            {
                [weakSelf setValue:dictionary[obj] forKey:obj];
            }
        }];
        
    }
    return self;
}

- (NSArray *)getPropertys
{
    NSMutableArray *propertys = [[NSMutableArray alloc] init];
    
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (i=0; i<outCount; i++)
    {
        objc_property_t property = properties[i];
        NSString * key = [[NSString alloc]initWithCString:property_getName(property)  encoding:NSUTF8StringEncoding];
        [propertys addObject:key];
    }
    
    return propertys;
}

- (NSDictionary *)toDictionary
{
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    
    //自己属性列表
    NSArray *keys = [self getPropertys];
    [keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id value = [self valueForKey:obj];
        if (value != nil)
        {
            [dictionary setObject:value forKey:obj];
        }
    }];
    
    return dictionary;
}

@end

 以后的mode就直接集成这个baseModel,然后调用初始化即可!!!!

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。