自动生成模型属性代码

假如一个字典中有很多属性,生成对应的模型要重复写很多@property (nonatomic, strong) ...这样的垃圾代码,怎样可以做到自动生成呢

简单的示例

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"status" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSArray *dictArray = dict[@"statuses"];
    NSDictionary *dict0 = dictArray[0];
    //遍历这个字典
    NSMutableString *propertyCodeM = [NSMutableString string];
    [dict0 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString *type = NSStringFromClass([obj class]);
        NSString *code;
        if ([type isEqualToString:@"__NSCFString"]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",key];
        }else if ([type isEqualToString:@"__NSCFNumber"]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSNumber *%@;",key];
        }else if ([type isEqualToString:@"__NSCFArray"]) {
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];
        }else if ([type isEqualToString:@"__NSCFDictionary"]){
            code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];
        }
        [propertyCodeM appendFormat:@"\n%@\n",code];
    }];
    NSLog(@"%@",propertyCodeM);

至此,打印出来的就是我们需要获取到的

自动生成模型属性代码_第1张图片
1.png

你可能感兴趣的:(自动生成模型属性代码)