iOS开发 Objective-C

IOS. 自动生成model属性。

2019-06-05  本文已影响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属性,直接复制粘贴即可

上一篇 下一篇

猜你喜欢

热点阅读