数据模型代码

/** * 自动生成解析数据的代码 */
- (void)resolveDict:(NSDictionary *)dict
{
    NSMutableString *propertyString = [NSMutableString string];

    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

        // 类型经常变,抽出来
        NSString *type;

        if ([obj isKindOfClass:NSClassFromString(@"__NSCFString")]) {
            type = @"NSString";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFArray")]){
            type = @"NSArray";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]){
            type = @"int";
        }else if ([obj isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){
            type = @"NSDictionary";
        }


        // 属性字符串
        NSString *str = nil;
        if ([type containsString:@"NSString"]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, copy) %@ *%@;",type,key];
        } else if ([type containsString:@"NS"]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",type,key];
        } else {
            str = [NSString stringWithFormat:@"@property (nonatomic, assign) %@ %@;",@"NSInteger",key];
        }

        // 每生成属性字符串,就自动换行。
        [propertyString appendFormat:@"\n%@\n",str];

    }];

    // 把拼接好的字符串打印出来,就好了。
    NSLog(@"%@",propertyString);
}

你可能感兴趣的:(String)