Skip to content

Commit

Permalink
fix the problem that value and property type are unmatched after conv…
Browse files Browse the repository at this point in the history
…ersions. (#742)
  • Loading branch information
wolfcon authored Oct 8, 2019
1 parent 3c6e9ab commit ccb6d47
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
if (![value isKindOfClass:[NSDecimalNumber class]]) {
value = [NSDecimalNumber decimalNumberWithDecimal:[((NSNumber *)value) decimalValue]];
}
} else if (propertyClass && ![value isKindOfClass:propertyClass]) { // value和property类型不匹配
}

// 经过转换后, 最终检查 value 与 property 是否匹配
if (propertyClass && ![value isKindOfClass:propertyClass]) {
value = nil;
}

Expand Down
42 changes: 42 additions & 0 deletions MJExtensionTests/MJExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,48 @@ @interface MJExtensionTests : XCTestCase

@implementation MJExtensionTests

#pragma mark 类型不匹配类型 -> 模型
- (void)testJSON2ModelUnmatched {
// 1.定义一个字典
NSDictionary *dict = @{
@"name" : @[],
@"icon" : @"lufy.png",
@"age" : @"2147483647",
@"age2": @"4294967295",
@"height" : @1.55,
@"money" : @"100.7777777",
@"sex" : @(SexFemale),
@"gay" : @"1",
@"speed" : @"120.5",
@"identifier" : @"9223372036854775807",
@"identifier2" : @"18446744073709551615",
@"price" : @"20.3",
@"rich" : @"2",
@"collect" : @"40个",
@"alien": @"yr Joking"
};

// 2.将字典转为MJUser模型
MJUser *user = [MJUser mj_objectWithKeyValues:dict];

// 3.检测
XCTAssert(!user.name);
XCTAssert([user.icon isEqual:@"lufy.png"]);
XCTAssert(user.age == INT_MAX);
XCTAssert(user.age2 == UINT_MAX);
XCTAssert([user.height isEqualToNumber:@(1.55)]);
XCTAssert([user.money compare:[NSDecimalNumber decimalNumberWithString:@"100.7777777"]] == NSOrderedSame);
XCTAssert(user.sex == SexFemale);
XCTAssert(user.gay);
XCTAssert(user.speed == 120);
XCTAssert(user.identifier == LONG_LONG_MAX);
XCTAssert(user.identifier2 == ULONG_LONG_MAX);
XCTAssert(user.price == 20.3);
XCTAssert(user.rich);
XCTAssert(user.collect == 40);
XCTAssert(!user.alien);
}

#pragma mark 简单的字典 -> 模型
- (void)testJSON2Model {
// 1.定义一个字典
Expand Down

0 comments on commit ccb6d47

Please sign in to comment.