之前遇到幾十個屬性的模型,簡直寫到惡心,所以找了這么個方法偷懶
這個方法需要不斷完善,畢竟里面的類型我只寫了幾個,到時候控制臺就會完整打印出來
+ (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);
}