在項(xiàng)目中轻抱,我們常常會(huì)用到數(shù)據(jù)模型,我們會(huì)用它來接收數(shù)據(jù)或者保存數(shù)據(jù)褐筛。你有沒有覺的每次使用的時(shí)候都很麻煩呢类少,要寫很多代碼?不要緊渔扎,我們可以寫一個(gè)數(shù)據(jù)模型的基礎(chǔ)類硫狞,每次創(chuàng)建的model類繼承它就可以方便使用了,是不是很簡(jiǎn)單呢晃痴!
BaseModel.h
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface BaseModel : NSObject
//接收數(shù)據(jù)使用
- (id)initWithDictionary:(NSDictionary*)jsonDic;
//歸檔專用
- (id)initWithCoder:(NSCoder *)aDecoder;
- (void)encodeWithCoder:(NSCoder *)aCoder;
@end
BaseModel.m
#import "BaseModel.h"
@implementation BaseModel
- (id)initWithDictionary:(NSDictionary*)jsonDic
{
if ((self = [super init]))
{
[self setValuesForKeysWithDictionary:jsonDic];
}
return self;
}
- (void)setValue:(id)value forKey:(NSString *)key
{
[super setValue:value forKey:key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"Undefined Key:%@ in %@",key,[self class]);
}
#pragma mark 數(shù)據(jù)持久化
- (void)encodeWithCoder:(NSCoder *)aCoder
{
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];
const char* char_f = property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue = [self valueForKey:(NSString *)propertyName];
if (propertyValue)
{
[aCoder encodeObject:propertyValue forKey:propertyName];
}
}
}
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super init];
if (self)
{
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];
const char* char_f = property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
NSString *capital = [[propertyName substringToIndex:1] uppercaseString];
NSString *setterSelStr = [NSString stringWithFormat:@"set%@%@:",capital,[propertyName substringFromIndex:1]];
SEL sel = NSSelectorFromString(setterSelStr);
[self performSelectorOnMainThread:sel
withObject:[aCoder decodeObjectForKey:propertyName]
waitUntilDone:[NSThread isMainThread]];
}
}
return self;
}
比如說我們有個(gè)用戶類UserModel
繼承了BaseModel
UserModel.h
#import "BaseModel.h"
@interface UserModel : BaseModel
@property (nonatomic , copy) NSString *user_name;
@property (nonatomic , copy) NSString *user_image;
@end
UserModel.m
#import "UserModel.h"
@implementation UserModel
-(void)setValue:(id)value forKey:(NSString *)key
{
[super setValue:[NSString stringWithFormat:@"%@",value] forKey:key];
}
@end
在控制器中使用模型接收數(shù)據(jù)
[self.manager POST:@"此處填上相應(yīng)的接口" parameters:params progress:^(NSProgress * _Nonnull uploadProgress)//params是接口的參數(shù)字典
{
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSDictionary *dic = responseObject[@"data"]//比如說返回的數(shù)據(jù)中data是我們要接受的數(shù)據(jù)
UserModel *model = [[UserModel alloc] initWithDictionary:dic];//此處接收數(shù)據(jù)
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"%@--",error);
}];
這樣我們就獲得了數(shù)據(jù)残吩,可以用它來對(duì)控件賦值,也可以+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
來歸檔數(shù)據(jù)倘核,而不用在遵守NSCoding
泣侮,寫相應(yīng)的方法了。是不是很簡(jiǎn)單了呢紧唱?
注:相關(guān)內(nèi)容我會(huì)繼續(xù)更新活尊。如果想找一些iOS方面的代碼可以關(guān)注我的簡(jiǎn)書,我會(huì)持續(xù)更新漏益,大家一起探討探討
在此謝謝大家閱讀