YYModel
YYModel copied to clipboard
modelCustomTransformFromDictionary 无法正常忽略嵌套的Model
`#import "ViewController.h" #import <YYModel.h> @interface Student : NSObject @property (strong, nonatomic) NSNumber* idNum; @property (strong, nonatomic) NSString* name; @end @implementation Student
- (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper { return @{@"idNum":@"id", @"name":@"name"}; } // json -> model
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic { if (_idNum && _name) { return YES; } return NO; } @end
@interface ClassRoom : NSObject @property (strong, nonatomic) NSNumber* idNum; @property (strong, nonatomic) NSArray<Student*>*students; @end @implementation ClassRoom
- (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper { return @{@"idNum":@"id", @"students":@"student_list"}; }
- (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass { return @{@"students":[Student class]}; } // json -> model
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic { if (_idNum) { return YES; } return NO; } @end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; NSDictionary* json = @{@"id":@302,@"student_list":@[@{@"id":@1,@"name":@"Cloud"},@{@"id":@2,@"name":@"Huang"},@{@"idNum":@3,@"nickname":@"cloudhuang"}]}; ClassRoom* room = [ClassRoom yy_modelWithJSON:json]; NSLog(@"%@",room); } @end `
我也发生了同样的问题,跟了一下源码,发现对于嵌套model的处理是这样的 NSObject *newOne = [cls new]; [newOne yy_modelSetWithDictionary:one]; if (newOne) [objectArr addObject:newOne];
或者 NSObject *newOne = [cls new]; [newOne yy_modelSetWithDictionary:(id)oneValue]; if (newOne) dic[oneKey] = newOne; 而yy_modelSetWithDictionary除非你自己讲self设置为nil,否则无法正常忽略,它不管yy_modelSetWithDictionary的返回值是否是YES
有几处调用了 yy_modelSetWithDictionary: 方法的地方忽略了返回值