项目介绍:
最近开始开源一点工作中用到的小工具,感谢大佬 暴走的鑫鑫 的指导。
源码在这里 ,或者使用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];