Skip to content

Commit

Permalink
add conversion support from French number string to number. (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcon authored Sep 26, 2019
1 parent d0b475b commit 7505398
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions MJExtension/NSObject+MJKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
*/
+ (NSDictionary *)mj_objectClassInArray;


/** 特殊地区在字符串格式化数字时使用 */
+ (NSLocale *)mj_numberLocale;

/**
* 旧值换新值,用于过滤字典中的值
*
Expand Down
8 changes: 7 additions & 1 deletion MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
NSArray *allowedPropertyNames = [clazz mj_totalAllowedPropertyNames];
NSArray *ignoredPropertyNames = [clazz mj_totalIgnoredPropertyNames];

NSLocale *numberLocale = nil;
if ([self.class respondsToSelector:@selector(mj_numberLocale)]) {
numberLocale = self.class.mj_numberLocale;
}

//通过封装的方法回调一个通过运行时编写的,用于返回属性列表的方法。
[clazz mj_enumerateProperties:^(MJProperty *property, BOOL *stop) {
@try {
Expand Down Expand Up @@ -175,7 +180,8 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
NSString *oldValue = value;

// NSString -> NSDecimalNumber, 使用 DecimalNumber 来转换数字, 避免丢失精度以及溢出
NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithString:oldValue locale:nil];
NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithString:oldValue
locale:numberLocale];

// 检查特殊情况
if (decimalValue == NSDecimalNumber.notANumber) {
Expand Down
6 changes: 6 additions & 0 deletions MJExtensionDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 0130EE7F233C56D8008D2386 /* MJFrenchUser.m */; };
2D2DB9CD2317DA07005A689E /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D2DB9CC2317DA07005A689E /* AppDelegate.m */; };
2D2DB9D02317DA07005A689E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D2DB9CF2317DA07005A689E /* ViewController.m */; };
2D2DB9D32317DA07005A689E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2D2DB9D12317DA07005A689E /* Main.storyboard */; };
Expand Down Expand Up @@ -83,6 +84,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJFrenchUser.h; sourceTree = "<group>"; };
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJFrenchUser.m; sourceTree = "<group>"; };
2D2DB9C82317DA07005A689E /* MJExtensionDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MJExtensionDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2D2DB9CB2317DA07005A689E /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
2D2DB9CC2317DA07005A689E /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -275,6 +278,8 @@
2D2DBA752317DBDF005A689E /* MJExtensionConfig.m */,
2D2DBA762317DBDF005A689E /* MJBook.h */,
2D2DBA772317DBDF005A689E /* MJPerson.m */,
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */,
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -463,6 +468,7 @@
2D2DBA822317DBE0005A689E /* MJStatus.m in Sources */,
2D2DBA832317DBE0005A689E /* MJAd.m in Sources */,
2D2DBA802317DBE0005A689E /* MJUser.m in Sources */,
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */,
2D2DBA852317DBE0005A689E /* MJPerson.m in Sources */,
2D2DBA7B2317DBE0005A689E /* MJBox.m in Sources */,
2D2DBA582317DBB9005A689E /* MJExtensionTests.m in Sources */,
Expand Down
13 changes: 7 additions & 6 deletions MJExtensionTests/MJExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "MJBook.h"
#import "MJBox.h"
#import <CoreData/CoreData.h>
#import "MJFrenchUser.h"

@interface MJExtensionTests : XCTestCase

Expand Down Expand Up @@ -65,24 +66,24 @@ - (void)testJSON2Model {
XCTAssert(user.collect == 40);
XCTAssert(!user.alien);
}

// 特殊模型, 数字为法语逗号为小数点的分隔符情况
- (void)testJSON2NumberModel {
// 1.定义一个字典
NSDictionary *dict = @{
@"age" : @"20",
@"height" : @1.55,
@"money" : @"100.9",
@"money" : @"100,9",
@"gay" : @"",
@"speed" : @"120.5",
@"speed" : @"120,5",
@"identifier" : @"3443623624362",
@"price" : @"20.3",
@"price" : @"20,3",
@"like" : @"20个",
@"collect" : @"收藏5",
@"rich" : @"hehe",
};

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

XCTAssert(user.age == 20);
XCTAssert(user.height.doubleValue == 1.55);
Expand Down
17 changes: 17 additions & 0 deletions MJExtensionTests/Model/MJFrenchUser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MJFrenchUser.h
// MJExtensionTests
//
// Created by Frank on 2019/9/26.
// Copyright © 2019 MJ Lee. All rights reserved.
//

#import "MJUser.h"

NS_ASSUME_NONNULL_BEGIN

@interface MJFrenchUser : MJUser

@end

NS_ASSUME_NONNULL_END
17 changes: 17 additions & 0 deletions MJExtensionTests/Model/MJFrenchUser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MJFrenchUser.m
// MJExtensionTests
//
// Created by Frank on 2019/9/26.
// Copyright © 2019 MJ Lee. All rights reserved.
//

#import "MJFrenchUser.h"

@implementation MJFrenchUser

+ (NSLocale *)mj_numberLocale {
return [NSLocale localeWithLocaleIdentifier:@"fr_FR"];
}

@end

0 comments on commit 7505398

Please sign in to comment.