之前遇到几十个属性的模型,简直写到恶心,所以找了这么个方法偷懒
这个方法需要不断完善,毕竟里面的类型我只写了几个,到时候控制台就会完整打印出来
+ (void)createPropertyCodeWithDic:(NSDictionary *)dic{
NSMutableString *string = [NSMutableString string];
[dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
NSLog(@"---%@ %@",propertyName,[value class]);
NSString *code;
if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",propertyName];
}else if([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName];
}else if([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName];
}else if([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",propertyName];
}else if([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName];
}
[string appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",string);
}
网友评论