DYModelMaker -- 进阶版的JSON Accelerator

最近开始开源一点工作中用到的小工具,感谢大佬 暴走的鑫鑫 的指导。
源码在这里 ,或者使用Cocoapod:

pod 'DYModelMaker'   

简介

@interface XYDHomeBannerModel : NSObject
@property (nonatomic, copy) NSArray *imgUrls;
@property (nonatomic, assign) double type;
@property (nonatomic, copy) NSString *linkId;
 。。。 
@end

上面这段代码还在一个一个属性的敲吗?要是有100个字段呢?要是数据结构四五六七层呢?DYModelMaker解放你的双手,一行代码生成所有属性!
对比JSON Accelerator优点:
1.生成常用第三方MJExtension和YYModel的系统关键字替换和数组中字典转模型代码。
2.归档不会生成一大段代码。
3.无需复制接口返回的json去转换。
4.模型头部可以添加关键字。

DYModelMaker目前有两类主要功能:

一.字典自动生成模型类。

  1. 支持多层模型嵌套。
  2. 自动生成两种框架(MJExtension和YYModel)的系统关键字替换和数组中字典转模型代码。
  3. 已经在类中实现了归档、解档,导入#DYModelMaker.h"即可实现模型存取,未使用DYModelMaker生成的模型同样适用。
    上代码,看见这个数据结构慌不慌?
NSDictionary *dic= @{@"id":@1,
                         @"data":@{@"app_id":@3,
                                   @"nextFlowId":@{@"test10":@""},
                                   @"approveResultValue":@[],
                                   @"title":@"加班流程",
                                   @"level":@1,
                                   @"flowinfo":@[@{@"reason":@"有其他事",
                                                   @"day_number":@6,
                                                   @"id":@(11),
                                                   @"time2":@"2017-03-03 11:28:00",
                                                   @"dept_name":@"技术部",
                                                   @"hour_number":@4}],
                                   @"flowTypeId":@4,
                                   @"department":@"技术部",
                                   @"result":@0,
                                   @"forword_emp_name":@"管理员 狗蛋 "}};
    
    [DYModelMaker DY_makeModelWithDictionary:dic modelKeyword:@"DY" modelName:@"testModel" ];

没关系,DYModelMaker一句代码搞定

[DYModelMaker DY_makeModelWithDictionary:dic          //数据源
                            modelKeyword:@"DY"         //模型名称前缀
                               modelName:@"testModel"]; //最外层模型名称,会自动加前缀

看结果-> _ ->

====================@interface==================

@class DYTestModel;
@class DYDataModel;
@class DYFlowinfoModel;
@class DYNextFlowIdModel;


@interface DYTestModel : NSObject
@property (nonatomic, copy) NSString *Id;
@property (nonatomic, strong) DYDataModel *data;
@end

@interface DYDataModel : NSObject
@property (nonatomic, copy) NSString *app_id;
@property (nonatomic, copy) NSString *flowTypeId;
@property (nonatomic, copy) NSArray *approveResultValue;
@property (nonatomic, copy) NSArray *flowinfo;
@property (nonatomic, copy) NSString *result;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *level;
@property (nonatomic, copy) NSString *forword_emp_name;
@property (nonatomic, copy) NSString *department;
@property (nonatomic, strong) DYNextFlowIdModel *nextFlowId;
@end

@interface DYFlowinfoModel : NSObject
@property (nonatomic, copy) NSString *day_number;
@property (nonatomic, copy) NSString *Id;
@property (nonatomic, copy) NSString *reason;
@property (nonatomic, copy) NSString *dept_name;
@property (nonatomic, copy) NSString *hour_number;
@property (nonatomic, copy) NSString *time2;
@end

@interface DYNextFlowIdModel : NSObject
@property (nonatomic, copy) NSString *test10;
@end

====================@implementation====================

@implementation DYTestModel

+(NSDictionary *)mj_replacedKeyFromPropertyName {
  return @{@"Id" : @"id"};

}
@end

@implementation DYDataModel

+ (NSDictionary *)mj_objectClassInArray{
 return @{ @"flowinfo" : @"DYFlowinfoModel" }; 
}
@end

@implementation DYFlowinfoModel

+(NSDictionary *)mj_replacedKeyFromPropertyName {
  return @{@"Id" : @"id"};

}
@end

@implementation DYNextFlowIdModel

@end

然后需要做的是复制粘贴,模型创建完成,就是这么简单。

ps: 在生成数字类型时根据不同习惯,可以选择生成NSNumber、NSString、double类型

//单利,只需配置一次,数字生成NSString
[DYModelMaker shareManager].numberType = DYModelNumberTypeString;
//可以选择使用MJExtension和YYMode字典转模型
[DYModelMaker shareManager].makerType = DYModelMakerTypeMJ;

二.模型间的赋值,都是用Runtime实现,原理简单,请看源码。

1)将model1 属性名相同的部分赋值给model2,不同部分互不影响

model0.testStr = @"testStr0";
model0.testNumber = @10;

DYTest1Model *model1 = [[DYTest1Model alloc] init];
model1.testStr = @"testStr1";

[DYModelMaker assignmentModel:model0 toModel:model1]; 

2)实现copy,深复制一个model

DYTestModel *model0 = [[DYTestModel alloc] init];
model0.testStr = @"testStr0";
model0.testNumber = @10;

DYTestModel *model1 = [DYModelMaker copyWithModel:model0];

3)将model0 和 model1 合并赋值到model2,model2中和model0、model1不同属性部分互不影响

DYTestModel *model0 = [[DYTestModel alloc] init];
model0.testNumber = @10;

DYTest1Model *model1 = [[DYTest1Model alloc] init];
model1.testStr = @"testStr1";

DYTest2Model *model2 = [[DYTest2Model alloc] init];
[DYModelMaker combineModelWithModel1:model0 model2:model1 toModel:model2];

4)判断model0和model1的所有属性值是否相等

DYTestModel *model0 = [[DYTestModel alloc] init];
    model0.testNumber = @10;
    model0.testStr = @"model0";
    
    DYTestModel *model1 = [[DYTestModel alloc] init];
    model1.testNumber = @11;
    model1.testStr = @"model1";
    
    DYTestModel *model2 = [[DYTestModel alloc] init];
    model2.testNumber = @10;
    model2.testStr = @"model0";
    
    NSLog(@"model0 %@ model1",[DYModelMaker isEqualModel1:model0 model2:model1]? @"等于" : @"不等于");
    NSLog(@"model0 %@ model2",[DYModelMaker isEqualModel1:model0 model2:model2]? @"等于" : @"不等于");

如果对你有帮助的话,能给 DYModelMaker 打赏一个小星星吗💕