Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持多维数组转化 #265

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions YYModel/NSObject+YYModel.m
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions YYModelTests/YYTestAutoTypeConvert.m
Expand Up @@ -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<NSArray<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}}";
Expand Down
3 changes: 3 additions & 0 deletions YYModelTests/YYTestCustomClass.m
Expand Up @@ -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]]);
Expand Down