標籤:blog io ar os 使用 sp for java on
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,然後調用初始化即可!!!!
ios 反射,MVC之model