CoderMJLee / MJExtension

A fast, convenient and nonintrusive conversion framework between JSON and model. Your model class doesn't need to extend any base class. You don't need to modify any model file.
MIT License
8.52k stars 2.16k forks source link

增加模型配置方法的继承保留或不保留选择方案 #836

Open wolfcon opened 2 years ago

wolfcon commented 2 years ago

mj_shouldAutoInheritConfigurations

原有方案

mj_setup block 设置会自动继承 mj_ 非 block 方法设置不会自动继承

逻辑

这里以 mj_replacedKeyFromPropertyName 为例:

/// 父类: 
@interface MJUser : NSObject <MJEConfiguration>
...
@end

@implementation MJUser
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{
        @"speed": @"runSpeed"
    };
}
@end

/// 子类:
@interface MJFrenchUser : MJUser
...
@end

@implementation MJFrenchUser
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{
        @"nickname": @"surnom"
    };
}
@end

原有逻辑

最终 MJUser 的替换列表为

@{
    @"speed": @"runSpeed"
};

而 MJFrenchUser 的替换列表为

@{
    @"nickname": @"surnom"
};

改进方案

提供选择继承与否的方法 mj_shouldAutoInheritConfigurations, 默认为继承, 如果设置为否. 则不保留继承, 与原方案一致 否则保留默认继承的话 MJFrenchUser 的替换列表应为


@{
    @"speed": @"runSpeed",
    @"nickname": @"surnom"
};