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

父类、子类中都存在嵌套模型的数组,该如何处理 #855

Open wv-y opened 1 year ago

wv-y commented 1 year ago

描述bug mj_objectClassInArray 只获取了当前类的配置,如果子类执行后,不会获取父类中的配置

image

目前我是这么处理的,是否还有其他办法呢?

@interface MyA : NSObject
@property (nonatomic, strong) NSArray *list;
@end

@implementation MyA

+ (NSDictionary *)mj_objectClassInArray {
    return @{
        @"list" : MyA.class
    };
}

@end

@interface MyB : MyA
@property (nonatomic, strong) NSArray *bList;
@end

@implementation MyB

+ (NSDictionary *)mj_objectClassInArray {
    NSDictionary *superDict = [super mj_objectClassInArray];
    NSMutableDictionary *dict = @{
        @"bList" : MyB.class
    }.mutableCopy;
    if (superDict) {
        [dict addEntriesFromDictionary:superDict];
    }
    return dict;
}

@end
wolfcon commented 1 year ago

你应该这样做:

  1. 首先判断父类是否实现了 mj_objectClassInArray
  2. 然后将父类跟子类进行合并