Skip to content

Commit

Permalink
Merge pull request #721 from kinarobin/fix_number_formatter_transform…
Browse files Browse the repository at this point in the history
…_bug

Fix number formatter transform bug
  • Loading branch information
CoderMJLee authored Aug 29, 2019
2 parents 52c53d6 + 7239ec5 commit a4ab19d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 1 addition & 4 deletions MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ + (BOOL)mj_isReferenceReplacedKeyWhenCreatingKeyValues
}

#pragma mark - --常用的对象--
static NSNumberFormatter *numberFormatter_;
+ (void)load
{
numberFormatter_ = [[NSNumberFormatter alloc] init];

// 默认设置
[self mj_referenceReplacedKeyWhenCreatingKeyValues:YES];
}
Expand Down Expand Up @@ -162,7 +159,7 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
if (type.typeClass == [NSDecimalNumber class]) {
value = [NSDecimalNumber decimalNumberWithString:oldValue];
} else {
value = [numberFormatter_ numberFromString:oldValue];
value = @([NSDecimalNumber decimalNumberWithString:oldValue].doubleValue);
}

// 如果是BOOL
Expand Down
2 changes: 2 additions & 0 deletions MJExtensionDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
INFOPLIST_FILE = MJExtensionDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -643,6 +644,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
INFOPLIST_FILE = MJExtensionDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
8 changes: 7 additions & 1 deletion MJExtensionTests/MJExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ - (void)testJSON2Model {
@"height" : @1.55,
@"money" : @"100.9",
@"sex" : @(SexFemale),
@"gay" : @"1"
@"gay" : @"1",
@"speed" : @"120.5",
@"identifier" : @"3443623624362",
@"price" : @"20.3",
// @"gay" : @"NO"
// @"gay" : @"true"
};
Expand All @@ -50,6 +53,9 @@ - (void)testJSON2Model {
XCTAssert(user.money.doubleValue == 100.9);
XCTAssert(user.sex == SexFemale);
XCTAssert(user.gay == YES);
XCTAssert(user.speed == 120);
XCTAssert(user.identifier == 3443623624362);
XCTAssert(user.price == 20.3);
}

#pragma mark JSON字符串 -> 模型
Expand Down
7 changes: 7 additions & 0 deletions MJExtensionTests/Model/MJUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ typedef enum {
@property (assign, nonatomic) Sex sex;
/** 同性恋 */
@property (assign, nonatomic, getter=isGay) BOOL gay;
/** 速度 */
@property (assign, nonatomic) NSInteger speed;
/** 标识 */
@property (assign, nonatomic) long long identifier;
/** 价格 */
@property (assign, nonatomic) double price;

@end

0 comments on commit a4ab19d

Please sign in to comment.