需要YYModel
- 1、简单新建Xcode工程
- 2、新建pch文件,方便导入公共类头文件
- 3、新建一个
BaseModel
类,继承NSObject
@interface BaseModel : NSObject
@property (nonatomic, strong) NSString *message;
@end
最简单的模型
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"message" : @"我是message"
};
// 转模型
BaseModel *model = [BaseModel modelWithJSON:json];
NSLog(@"model.message = %@",model.message);
}
@end
7A55A03F-3D73-4574-8314-A7CB2A687609.png
有关键字的模型
.h文件
@interface BaseModel : NSObject
@property (nonatomic, strong) NSString *ID;
@property (nonatomic, strong) NSString *message;
@end
.m文件,进行映射
@implementation BaseModel
+ (NSDictionary *)modelCustomPropertyMapper{
return @{
@"ID" : @"id"
};
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"id" : @"123",
@"message" : @"我是message"
};
// 转模型
BaseModel *model = [BaseModel modelWithJSON:json];
NSLog(@"model.message = %@",model.message);
NSLog(@"model.ID = %@",model.ID);
}
@end
08C4925F-4CC3-473B-87A2-6B1F448419F1.png
模型嵌套模型
- 1、新建一个
AModel
类,继承NSObject
- 2、为
AModel
新增两个属性分别为title
、base
@interface AModel : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) BaseModel *base;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"title" : @"我是标题",
@"base" : @{
@"id" : @"123",
@"message" : @"我是message"
}
};
// 转模型
AModel *model = [AModel modelWithJSON:json];
NSLog(@"model.title = %@",model.title);
NSLog(@"model.base = %@",model.base);
NSLog(@"model.base.ID = %@",model.base.ID);
NSLog(@"model.base.message = %@",model.base.message);
}
@end
59965174-166E-456E-8DC4-4F1B6696B7E6.png
简单的模型继承
- 1、新建一个
BModel
类,继承BaseModel
- 2、为
BModel
新增一个属性title
,其他属性都从BaseModel
继承而来
@interface BModel : BaseModel
@property (nonatomic, strong) NSString *title;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"title" : @"我是标题",
@"id" : @"123",
@"message" : @"我是message"
};
// 转模型
BModel *model = [BModel modelWithJSON:json];
NSLog(@"model.title = %@",model.title);
NSLog(@"model.ID = %@",model.ID);
NSLog(@"model.message = %@",model.message);
}
@end
BC91F854-609E-4051-B272-DD3E540EA868.png
多映射的模型继承
- 1、新建一个
CModel
类,继承BaseModel
- 2、为
CModel
新增两个属性myColor
、mySize
(后台返回是my_color
、my_size
),其他属性都从BaseModel
继承而来
.h文件,将modelCustomPropertyMapper
暴露出来
@interface BaseModel : NSObject
@property (nonatomic, strong) NSString *ID;
@property (nonatomic, strong) NSString *message;
+ (NSDictionary *)modelCustomPropertyMapper;
@end
.m文件,正常映射
@implementation BaseModel
+ (NSDictionary *)modelCustomPropertyMapper{
return @{
@"ID" : @"id"
};
}
@end
.h文件
@interface CModel : BaseModel
@property (nonatomic, strong) NSString *myColor;
@property (nonatomic, strong) NSString *mySize;
@end
.m文件,往父类进行映射
@implementation CModel
+ (NSDictionary *)modelCustomPropertyMapper
{
NSMutableDictionary *superDictM = [[super modelCustomPropertyMapper] mutableCopy];
[superDictM setObject:@"my_color" forKey:@"myColor"];
[superDictM setObject:@"my_size" forKey:@"mySize"];
return superDictM;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"id" : @"123",
@"message" : @"我是message",
@"my_color" : @"我的颜色",
@"my_size" : @"我的大小"
};
// 转模型
CModel *model = [CModel modelWithJSON:json];
NSLog(@"model.ID = %@",model.ID);
NSLog(@"model.message = %@",model.message);
NSLog(@"model.myColor = %@",model.myColor);
NSLog(@"model.mySize = %@",model.mySize);
}
@end
9238ECC0-5658-4014-9DFD-41AD47868F00.png
更复杂的模型继承
- 1、首先修改
AModel
模型,使其简单的继承NSObject
,并且只拥有一个title
属性
@interface AModel : NSObject
@property (nonatomic, strong) NSString *title;
@end
- 2、其次修改
BModel
模型,使其简单的继承NSObject
,并且也拥有一个desc
属性
@interface BModel : NSObject
@property (nonatomic, strong) NSString *desc;
@end
- 3、修改基类
BaseModel
模型,除了简单的ID
、message
,还新增拥有多个AModel
的数组aModels
@interface BaseModel : NSObject
@property (nonatomic, strong) NSString *ID;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSArray<AModel *> *aModels;
+ (NSDictionary *)modelCustomPropertyMapper;
+ (NSDictionary *)modelContainerPropertyGenericClass;
@end
@implementation BaseModel
+ (NSDictionary *)modelCustomPropertyMapper{
return @{
@"ID" : @"id"
};
}
+ (NSDictionary *)modelContainerPropertyGenericClass{
return @{
@"aModels" : [AModel class]
};
}
@end
- 4、新建
DModel
模型,继承BaseModel
,使其拥有BaseModel
的所以属性,额外新增myPower
和拥有多个BModel
的数组bModels
@interface DModel : BaseModel
@property (nonatomic, strong) NSString *myPower;
@property (nonatomic, strong) NSArray<BModel *> *bModels;
@end
@implementation DModel
+ (NSDictionary *)modelCustomPropertyMapper
{
NSMutableDictionary *superDictM = [[super modelCustomPropertyMapper] mutableCopy];
[superDictM setObject:@"my_power" forKey:@"myPower"];
return superDictM;
}
+ (NSDictionary *)modelContainerPropertyGenericClass {
NSMutableDictionary *superDictM = [[super modelContainerPropertyGenericClass] mutableCopy];
[superDictM setObject:[BModel class] forKey:@"bModels"];
return superDictM;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 假数据
NSDictionary *json = @{
@"id" : @"123",
@"message" : @"我是message",
@"my_power" : @"我的力量",
@"aModels" : @[
@{@"title":@"我是A模型的title"}
],
@"bModels" : @[
@{@"desc":@"我是B模型的desc"}
]
};
// 转模型
DModel *model = [DModel modelWithJSON:json];
NSLog(@"model.ID = %@",model.ID);
NSLog(@"model.message = %@",model.message);
NSLog(@"model.myPower = %@",model.myPower);
NSLog(@"---------------------");
NSLog(@"model.aModels = %@",model.aModels);
AModel *aModel = [model.aModels firstObject];
NSLog(@"model.aModels.title = %@",aModel.title);
NSLog(@"---------------------");
NSLog(@"model.bModels = %@",model.bModels);
BModel *bModel = [model.bModels firstObject];
NSLog(@"model.bModels.desc = %@",bModel.desc);
}
@end
177EC767-9FEC-41A1-A6AF-66FF6364FB16.png
至此,基本常见的数据转模型都已成功,打印也有值
网友评论