ibireme / YYKit

A collection of iOS components.
MIT License
13.99k stars 3.69k forks source link

YYModel多层级模型嵌套后,子层级无法使用- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic过滤数据 #577

Open Geetyty opened 3 years ago

Geetyty commented 3 years ago

A模型:-------------------------------------------------------- @interface AModel : NSObject

@property (nonatomic, retain) NSString name; @property (nonatomic, assign) NSUInteger age; @property (nonatomic, retain) NSArray<BModel > *list;

@end

@implementation AModel

@end

B模型:-------------------------------------------------------- @interface BModel : NSObject

@property (nonatomic, retain) NSString *courseName; @property (nonatomic, assign) float score;

@end

@implementation BModel

@end

数据:--------------------------------------------------------- NSDictionary studentDic = @{ @"name" : @"XXXX", @"age" : @"29", @"list" : @[@{ @"courseName" : @"语文", @"score" : @"30" },@{ @"courseName" : @"数学", @"score" : @"100" },@{ @"courseName" : @"英语", @"score" : @"60" }] }; AModel student = [AModel modelWithJSON:studentDic];

期望与结果:-------------------------------------------------- 应该可以过滤掉不足50分的科目,但实际list中三个模型均生成了

源码观察:---------------------------------------------------- NSObject newOne = [cls new]; [newOne modelSetWithDictionary:one]; if (newOne) [objectArr addObject:newOne]; 上方源码导致全部解析,因为会必然添加模型,是否应该改为 NSObject newOne = [cls new]; if ([newOne modelSetWithDictionary:one]) [objectArr addObject:newOne];