DYModelMaker — ioser实用的模型自动生成器

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

pod 'DYModelMaker'   

目前有两个主要功能:

一.字典生成模型,支持多层模型嵌套,自动生成两种框架(MJExtension和YYModel)的系统关键字替换和数组中字典转模型代码。

已经在类中实现了归档、解档,导入#import "NSObject+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":@"管理员 狗蛋 "}};
    
    [NSObject DY_makeModelWithDictionary:dic modelKeyword:@"DY" modelName:@"testModel"  makeType:DYModelMakerTypeMJ];

直接看结果-> _ ->

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

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


@interface DYTestModel : NSObject
@property (nonatomic, strong) NSNumber *testModelId;
@property (nonatomic, strong) DataModel *data;
@end

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

@interface DYFlowinfoModel : NSObject
@property (nonatomic, strong) NSNumber *day_number;
@property (nonatomic, strong) NSNumber *flowinfoModelId;
@property (nonatomic, copy) NSString *reason;
@property (nonatomic, copy) NSString *dept_name;
@property (nonatomic, strong) NSNumber *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

然后需要做的是复制粘贴,就是这么简单。

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

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

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

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

[NSObject assignmentModel:model0 toModel:model1]; 

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

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

DYTestModel *model1 = [NSObject 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];
[NSObject combineModelWithModel1:model0 model2:model1 toModel:model2];

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值