基于的模型和繼承的模型 Base model and inherited model
@interface BaseUser : BaseDataModel
@property (copy, nonatomic, nullable) NSString<Optional> *nick;
@property (strong, nonatomic, nullable) NSNumber<Optional> *age;
@property (strong, nonatomic, nullable) NSNumber<Optional> *sex;
@property (strong, nonatomic, nullable) NSNumber<Optional> *level;
@property (strong, nonatomic, nullable) NSNumber<Optional> *score;
@end
@interface Staff : BaseUser
@property (strong, nonatomic, nullable) NSNumber<Optional> *memberLevel;
@property (strong, nonatomic, nullable) NSNumber<Optional> *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: 當(dāng)基類沒有具體實(shí)現(xiàn)鍵值映射愕秫,應(yīng)當(dāng)由子類實(shí)現(xiàn)抗楔,此舉可以讓不同的子類映射不同的值
@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));
}
相關(guān)
- 詳見極致框架官網(wǎng)<extreme.framework/BaseDataModel.h>的介紹扯再。通過極致框架官網(wǎng)頂部的搜索功能搜索 BaseDataModel臼节。
許可
- 本文采用 BY-NC-SA 許可協(xié)議蝠筑。即:署名——轉(zhuǎn)載請注明出處;非商業(yè)使用咆瘟;相同方式傳播——再分發(fā)的文章許可與原文相同瓤鼻。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者