diff --git a/YYModel/NSObject+YYModel.m b/YYModel/NSObject+YYModel.m index 3d7c470..30a26b4 100644 --- a/YYModel/NSObject+YYModel.m +++ b/YYModel/NSObject+YYModel.m @@ -902,6 +902,18 @@ static void ModelSetValueForProperty(__unsafe_unretained id model, for (id one in valueArr) { if ([one isKindOfClass:meta->_genericCls]) { [objectArr addObject:one]; + } else if (meta->_genericCls == [NSString class] && [one isKindOfClass:[NSNumber class]]) { + [objectArr addObject:((NSNumber *)one).stringValue]; + } else if (meta->_genericCls == [NSMutableString class] && [one isKindOfClass:[NSNumber class]]) { + [objectArr addObject:((NSNumber *)one).stringValue.mutableCopy]; + } else if (meta->_genericCls == [NSNumber class]) { + [objectArr addObject:YYNSNumberCreateFromID(one)]; + } else if (meta->_genericCls == [NSURL class] && [one isKindOfClass:[NSString class]]) { + NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet]; + NSString *str = [one stringByTrimmingCharactersInSet:set]; + if (str.length > 0) { + [objectArr addObject:[[NSURL alloc] initWithString:str]]; + } } else if ([one isKindOfClass:[NSDictionary class]]) { Class cls = meta->_genericCls; if (meta->_hasCustomClassFromDictionary) { diff --git a/YYModelTests/YYTestCustomClass.m b/YYModelTests/YYTestCustomClass.m index 3b69532..e8b17be 100644 --- a/YYModelTests/YYTestCustomClass.m +++ b/YYModelTests/YYTestCustomClass.m @@ -47,6 +47,11 @@ @interface YYTestCustomClassModel : NSObject @property (nonatomic, strong) NSDictionary *userDict; @property (nonatomic, strong) NSSet *userSet; @property (nonatomic, strong) YYBaseUser *user; +@property (nonatomic, strong) NSArray *imageURLs; +@property (nonatomic, strong) NSArray *phones; +@property (nonatomic, strong) NSArray *editPhones; +@property (nonatomic, strong) NSArray *scores; + @end @implementation YYTestCustomClassModel @@ -54,7 +59,12 @@ @implementation YYTestCustomClassModel + (NSDictionary *)modelContainerPropertyGenericClass { return @{@"users" : YYBaseUser.class, @"userDict" : YYBaseUser.class, - @"userSet" : YYBaseUser.class}; + @"userSet" : YYBaseUser.class, + @"imageURLs" : NSURL.class, + @"phones" : NSString.class, + @"editPhones" : NSMutableString.class, + @"scores" : NSNumber.class, + }; } + (Class)modelCustomClassForDictionary:(NSDictionary*)dictionary { if (dictionary[@"localName"]) { @@ -80,6 +90,9 @@ - (void)test { NSDictionary *jsonUserBase = @{@"uid" : @123, @"name" : @"Harry"}; NSDictionary *jsonUserLocal = @{@"uid" : @123, @"name" : @"Harry", @"localName" : @"HarryLocal"}; NSDictionary *jsonUserRemote = @{@"uid" : @123, @"name" : @"Harry", @"remoteName" : @"HarryRemote"}; + NSArray *jsonImageURLs = @[@"http://aaa.com", @"http://bbb.com", @""]; + NSArray *jsonPhones = @[@13000000001, @13000000002, @"13000000003"]; + NSArray *jsonScores = @[@"90", @"80", @"70"]; user = [YYBaseUser yy_modelWithDictionary:jsonUserBase]; XCTAssert([user isMemberOfClass:[YYBaseUser class]]); @@ -90,6 +103,20 @@ - (void)test { user = [YYBaseUser yy_modelWithDictionary:jsonUserRemote]; XCTAssert([user isMemberOfClass:[YYRemoteUser class]]); + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"imageURLs" : jsonImageURLs}]; + XCTAssert([model.imageURLs[0] isMemberOfClass:[NSURL class]]); + + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"phones" : jsonPhones}]; + XCTAssert([model.phones[0] isKindOfClass:[NSString class]]); + + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"editPhones" : jsonPhones}]; + //__NSCFConstantString->__NSCFString->NSMutableString->NSString + //model.editPhones[2]不是 NSMutableString,使用 `appendString:` 会导致 crash + XCTAssert([model.editPhones[2] isKindOfClass:[NSMutableString class]]); + + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"scores" : jsonScores}]; + XCTAssert([model.scores[0] isKindOfClass:[NSNumber class]]); + model = [YYTestCustomClassModel yy_modelWithJSON:@{@"user" : jsonUserLocal}]; XCTAssert([model.user isMemberOfClass:[YYLocalUser class]]);