diff --git a/YYModel/NSObject+YYModel.m b/YYModel/NSObject+YYModel.m index 3d7c470..6aca30c 100644 --- a/YYModel/NSObject+YYModel.m +++ b/YYModel/NSObject+YYModel.m @@ -911,6 +911,9 @@ static void ModelSetValueForProperty(__unsafe_unretained id model, NSObject *newOne = [cls new]; [newOne yy_modelSetWithDictionary:one]; if (newOne) [objectArr addObject:newOne]; + } else if ([one isKindOfClass:[NSArray class]]) { + NSArray *objectArrInner = [NSArray yy_modelArrayWithClass:meta->_genericCls json:one]; + [objectArr addObject:objectArrInner]; } } ((void (*)(id, SEL, id))(void *) objc_msgSend)((id)model, meta->_setter, objectArr); @@ -1795,9 +1798,13 @@ + (NSArray *)yy_modelArrayWithClass:(Class)cls array:(NSArray *)arr { if (!cls || !arr) return nil; NSMutableArray *result = [NSMutableArray new]; for (NSDictionary *dic in arr) { - if (![dic isKindOfClass:[NSDictionary class]]) continue; - NSObject *obj = [cls yy_modelWithDictionary:dic]; - if (obj) [result addObject:obj]; + if ([dic isKindOfClass:[NSDictionary class]]) { + NSObject *obj = [cls yy_modelWithDictionary:dic]; + if (obj) [result addObject:obj]; + } else if ([dic isKindOfClass:[NSArray class]]) { + NSArray *objectArrInner = [NSArray yy_modelArrayWithClass:cls json:dic]; + [result addObject:objectArrInner]; + } } return result; } diff --git a/YYModelTests/YYTestAutoTypeConvert.m b/YYModelTests/YYTestAutoTypeConvert.m index 2de2d2f..6c1ac76 100644 --- a/YYModelTests/YYTestAutoTypeConvert.m +++ b/YYModelTests/YYTestAutoTypeConvert.m @@ -430,6 +430,20 @@ - (void)testArrayAndDic { array = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonObjectFromString:json]]; XCTAssertTrue(array.count == 3); XCTAssertTrue([array.firstObject isKindOfClass:[YYTestAutoTypeModel class]]); + + + json = @"[[[{\"v\":1},{\"v\":2},{\"v\":3}]]]"; + NSArray *> *mutiArray = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:json]; + XCTAssertTrue(mutiArray.count == 1); + XCTAssertTrue([mutiArray.firstObject.firstObject.firstObject isKindOfClass:[YYTestAutoTypeModel class]]); + + mutiArray = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonDataFromString:json]]; + XCTAssertTrue(mutiArray.count == 1); + XCTAssertTrue([mutiArray.firstObject.firstObject.firstObject isKindOfClass:[YYTestAutoTypeModel class]]); + + mutiArray = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonObjectFromString:json]]; + XCTAssertTrue(mutiArray.count == 1); + XCTAssertTrue([mutiArray.firstObject.firstObject.firstObject isKindOfClass:[YYTestAutoTypeModel class]]); json = @"{\"a\":{\"v\":1},\"b\":{\"v\":2},\"c\":{\"v\":3}}"; diff --git a/YYModelTests/YYTestCustomClass.m b/YYModelTests/YYTestCustomClass.m index 3b69532..a7e75f6 100644 --- a/YYModelTests/YYTestCustomClass.m +++ b/YYModelTests/YYTestCustomClass.m @@ -98,6 +98,9 @@ - (void)test { XCTAssert([model.users[0] isMemberOfClass:[YYBaseUser class]]); XCTAssert([model.users[1] isMemberOfClass:[YYLocalUser class]]); XCTAssert([model.users[2] isMemberOfClass:[YYRemoteUser class]]); + + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"users" : @[@[jsonUserBase]]}]; + XCTAssert([((NSArray *)(model.users[0]))[0] isMemberOfClass:[YYBaseUser class]]); model = [YYTestCustomClassModel yy_modelWithJSON:@{@"userDict" : @{@"a" : jsonUserBase, @"b" : jsonUserLocal, @"c" : jsonUserRemote}}]; XCTAssert([model.userDict[@"a"] isKindOfClass:[YYBaseUser class]]);