美文网首页
如何偷懒快速创建model,不用一个个写属性

如何偷懒快速创建model,不用一个个写属性

作者: l富文本l | 来源:发表于2017-07-24 18:21 被阅读0次

    之前遇到几十个属性的模型,简直写到恶心,所以找了这么个方法偷懒
    这个方法需要不断完善,毕竟里面的类型我只写了几个,到时候控制台就会完整打印出来

    + (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);
    }
    
    

    相关文章

      网友评论

          本文标题:如何偷懒快速创建model,不用一个个写属性

          本文链接:https://www.haomeiwen.com/subject/swabkxtx.html