字典转换成模型属性代码笔记

来源:互联网 发布:win10软件不见了 编辑:程序博客网 时间:2024/06/10 08:33

创建一个字典的Category:NSDictionary+Property

- (void)getPropertyCodeFromDict{    NSMutableString *codes = [NSMutableString string];    // 遍历字典    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {        NSString *code;        if ([value isKindOfClass:[NSString class]]) {            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",key];        } else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {            code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];        } else if ([value isKindOfClass:[NSNumber class]]) {            code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",key];        } else if ([value isKindOfClass:[NSArray class]]) {            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];        } else if ([value isKindOfClass:[NSDictionary class]]) {            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];        }        [codes appendFormat:@"\n%@\n",code];    }];    NSLog(@"%@",codes);}
0 0
原创粉丝点击