iOS 后台返回字段"-"

由于历史原因,后台返回的很多字段都包含"-",这对于我们封装 Model 很不利,下面通过重写

-(void)setValue:(id)value forUndefinedKey:(NSString *)key

方法来把"-"替换掉。

以下是 baseModel,让其他的 model 全部继承 baseModel 即可。

-(instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    if (self = [super init]) {
        if ([dictionary isKindOfClass:[NSDictionary class]]) {
            self.originSource_dic = [NSDictionary dictionaryWithDictionary:dictionary];
            [self setValuesForKeysWithDictionary:dictionary];
            
        }else{
            MyNSLog(@"---BasicModel 填装失败-\n-错误原因---dictionary===%@",dictionary);
        }
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    NSArray* compont = [key componentsSeparatedByString:@"-"];
    if (compont.count > 1) {
        NSString* property = [compont componentsJoinedByString:@""];
        [self setValue:value forKey:property];
    }
}

你可能感兴趣的:(iOS 后台返回字段"-")