iOS模型

作者: HCL黄 | 来源:发表于2019-12-19 16:57 被阅读0次

需要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新增两个属性分别为titlebase
@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新增两个属性myColormySize(后台返回是my_colormy_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模型,除了简单的IDmessage,还新增拥有多个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
  • 5、修改ViewController的假数据
@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

至此,基本常见的数据转模型都已成功,打印也有值

相关文章

网友评论

      本文标题:iOS模型

      本文链接:https://www.haomeiwen.com/subject/sdnmnctx.html