在APP开发中,声明模型类文件真的是一个出力无脑无技术的活,但又是必须要做的,网上有自动解析JSON生成模型类文件的,也有用脚本的,今天我提供在工程中使用的一种方式,就是给字典加一个extersion,每一个字典,调用这个方法就能打印出对应的模型类所有属性的声明了,然后复制黏贴就行,操作还是挺简单的,具体代码如下:
- (void)yq_printModelWithReplacePropertyType:(NSDictionary*)replaceDict modelName:(NSString*)modelName {
NSArray*replacePropertyArray = [replaceDictallKeys];
NSArray*newPropertyArray = [replaceDictallValues];
__block NSString *log = [NSString stringWithFormat:@"\n---------- %@ 开始打印 ----------\n", modelName];
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
// 确定引用
NSString*reference =@"<#reference";
// 确定类型
NSString*type =@"<#type";
// 确定属性名
NSString*protertyName = key;
// 确定要替换的类型
if([replacePropertyArraycontainsObject:key]) {
reference =@"strong";
NSIntegerindex = [replacePropertyArrayindexOfObject:key];
idvalue = newPropertyArray[index];
if([valueisKindOfClass:[NSStringclass]]) {
type = value;
if([valueisEqualToString:@"BOOL"]) {
reference =@"assign";
}
}
}elseif([objisKindOfClass:[NSNumberclass]]) {// 数字
reference =@"assign";
inta1 = [objintValue];
inta2 = [objdoubleValue];
if((a2 - a1) ==0) {
type =@"NSInteger";
}else{
type =@"CGFloat";
}
}elseif([objisKindOfClass:[NSStringclass]]) {// 字符串
reference =@"copy";
type =@"NSString";
}elseif([objisKindOfClass:[NSDictionaryclass]]) {// 字典
reference =@"strong";
}elseif([objisKindOfClass:[NSArrayclass]]) {// 数组
reference =@"strong";
type = [NSString stringWithFormat:@"NSArray<<#paradigm%@%@>", @"#", @">"];
}
if([referenceisEqualToString:@"<#reference"]) {
reference = [NSStringstringWithFormat:@"%@#>", reference];
}
if([typeisEqualToString:@"<#type"]) {
type = [NSStringstringWithFormat:@"%@#>", type];
}
if([referenceisEqualToString:@"strong"] || [referenceisEqualToString:@"copy"]) {
protertyName = [NSStringstringWithFormat:@"*%@", key];
}
NSString *protertyStr = [NSString stringWithFormat:@"\n/**\n<#Description%@>\n*/\n@property (nonatomic, %@) %@ %@;\n", @"#", reference, type, protertyName];
log = [NSStringstringWithFormat:@"%@%@", log, protertyStr];
}];
// 打印
NSLog(@"%@", log);
}