美文网首页iOS开发 Objective-C
IOS. 自动生成model属性。

IOS. 自动生成model属性。

作者: 彼岸花下的暗影 | 来源:发表于2019-06-05 14:47 被阅读0次

当后台给的参数过多时。可以使用这个分类

传入一个字典,给你一个model属性的代码(可根据自己的参数类型做更改)。

.h文件

@interface NSObject (Property)

+ (void)createPropertyCodeWithDict:(NSDictionary *)dict;


@end

.m文件

@implementation NSObject (Property)

+ (void)createPropertyCodeWithDict:(NSDictionary *)dict
{
    
    NSMutableString *strM = [NSMutableString string];

    // 遍历字典
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull propertyName, id  _Nonnull value, BOOL * _Nonnull stop) {
        //        NSLog(@"%@ %@",propertyName,[value class]);
        NSString *code;
        
         NSLog(@"%@",[value class]);
        
        if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSDictionary *%@;",propertyName]
            ;
        }else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
            ;
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSArray0")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
            ;
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSArrayI")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName]
            ;
        }
        
        else if ([value isKindOfClass:NSClassFromString(@"NSTaggedPointerString")]){
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName]
            ;
        }
        [strM appendFormat:@"\n%@\n",code];
        
       
        
    }];
    
     NSLog(@"%@",strM);
  
}

@end

用法,直接传入一个字典,会在控制台打印model属性,直接复制粘贴即可

相关文章

网友评论

    本文标题:IOS. 自动生成model属性。

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