基于的模型和继承的模型 Base model and inherited model
@interface BaseUser : BaseDataModel
@property (copy, nonatomic, nullable) NSString *nick;
@property (strong, nonatomic, nullable) NSNumber *age;
@property (strong, nonatomic, nullable) NSNumber *sex;
@property (strong, nonatomic, nullable) NSNumber *level;
@property (strong, nonatomic, nullable) NSNumber *score;
@end
@interface Staff : BaseUser
@property (strong, nonatomic, nullable) NSNumber *memberLevel;
@property (strong, nonatomic, nullable) NSNumber *memberScore;
@end
@interface Customer : BaseUser
@end
@implementation Staff
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
@"dataID": @"ID", // dataID用作ID
@"dataName": @"name", // dataName用作name
@"level": @"staffLevel", // level用作职员等级
@"score": @"staffScore", // score用作职员得分
@"memberLevel": @"level", // memberLevel用作会员等级
@"memberScore": @"score" // memberScore用作会员积分
}];
}
@end
@implementation Customer
+(JSONKeyMapper*)keyMapper
{
return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
@"dataID": @"ID", // dataID用作ID
@"dataName": @"nick", // dataName用作nick
@"level": @"level", // level用作会员等级
@"score": @"score" // score用作会员积分
}];
}
@end
@implementation BaseUser
//TODO: 当基类没有具体实现键值映射,应当由子类实现,此举可以让不同的子类映射不同的值
@end
//Test
NSDictionary *dictionary = @{@"ID": @59,
@"name": [NSNull null],
@"nick": @"Tony",
@"age": [NSNull null],
@"level": @8,
@"score": @666,
@"staffLevel": @15,
@"staffScore": @86,
@"offline": @true};
NSError *error;
Staff *model1 = [[Staff alloc] initWithDictionary:dictionary error:&error];
if (model1) {
LOG_FORMAT(@"%@", STRING_FORMAT(@"Staff model: %@", model1));
}
Customer *model2 = [[Customer alloc] initWithDictionary:dictionary error:&error];
if (model2) {
LOG_FORMAT(@"%@", STRING_FORMAT(@"Customer model: %@", model2));
}
相关
- 详见极致框架官网的介绍。通过极致框架官网顶部的搜索功能搜索 BaseDataModel。
许可
- 本文采用 BY-NC-SA 许可协议。即:署名——转载请注明出处;非商业使用;相同方式传播——再分发的文章许可与原文相同。
查看原文