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

Fix decimal number transform to bool bug #726

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
if (type.typeClass == [NSDecimalNumber class]) {
value = [NSDecimalNumber decimalNumberWithString:oldValue];
} else {
value = @([NSDecimalNumber decimalNumberWithString:oldValue].doubleValue);
NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithString:oldValue];
value = decimalValue == [NSDecimalNumber notANumber] ? @(0) : @(decimalValue.doubleValue);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// 如果是BOOL
Expand Down
36 changes: 34 additions & 2 deletions MJExtensionTests/MJExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ - (void)testJSON2Model {
@"speed" : @"120.5",
@"identifier" : @"3443623624362",
@"price" : @"20.3",
// @"gay" : @"NO"
// @"gay" : @"true"
@"rich" : @"2",
@"collect" : @"40个"
};

// 2.将字典转为MJUser模型
Expand All @@ -56,6 +56,38 @@ - (void)testJSON2Model {
XCTAssert(user.speed == 120);
XCTAssert(user.identifier == 3443623624362);
XCTAssert(user.price == 20.3);
XCTAssert(user.rich == YES);
XCTAssert(user.collect == 40);
}

- (void)testJSON2NumberModel {
// 1.定义一个字典
NSDictionary *dict = @{
@"age" : @"20",
@"height" : @1.55,
@"money" : @"100.9",
@"gay" : @"",
@"speed" : @"120.5",
@"identifier" : @"3443623624362",
@"price" : @"20.3",
@"like" : @"20个",
@"collect" : @"收藏5",
@"rich" : @"hehe",
};

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

XCTAssert(user.age == 20);
XCTAssert(user.height.doubleValue == 1.55);
XCTAssert(user.money.doubleValue == 100.9);
XCTAssert(user.gay == NO);
XCTAssert(user.speed == 120);
XCTAssert(user.identifier == 3443623624362);
XCTAssert(user.price == 20.3);
XCTAssert(user.like == 20);
XCTAssert(user.collect == 0);
XCTAssert(user.rich == NO);
}

#pragma mark JSON字符串 -> 模型
Expand Down
6 changes: 6 additions & 0 deletions MJExtensionTests/Model/MJUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ typedef enum {
@property (assign, nonatomic) long long identifier;
/** 价格 */
@property (assign, nonatomic) double price;
/** 赞 */
@property (assign, nonatomic) int like;
/** 收藏 */
@property (assign, nonatomic) int collect;
/** 富有 */
@property (assign, nonatomic) BOOL rich;

@end