Skip to content

Commit 7505398

Browse files
authored
add conversion support from French number string to number. (#737)
1 parent d0b475b commit 7505398

File tree

6 files changed

+58
-7
lines changed

6 files changed

+58
-7
lines changed

MJExtension/NSObject+MJKeyValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
*/
4848
+ (NSDictionary *)mj_objectClassInArray;
4949

50+
51+
/** 特殊地区在字符串格式化数字时使用 */
52+
+ (NSLocale *)mj_numberLocale;
53+
5054
/**
5155
* 旧值换新值,用于过滤字典中的值
5256
*

MJExtension/NSObject+MJKeyValue.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
100100
NSArray *allowedPropertyNames = [clazz mj_totalAllowedPropertyNames];
101101
NSArray *ignoredPropertyNames = [clazz mj_totalIgnoredPropertyNames];
102102

103+
NSLocale *numberLocale = nil;
104+
if ([self.class respondsToSelector:@selector(mj_numberLocale)]) {
105+
numberLocale = self.class.mj_numberLocale;
106+
}
107+
103108
//通过封装的方法回调一个通过运行时编写的,用于返回属性列表的方法。
104109
[clazz mj_enumerateProperties:^(MJProperty *property, BOOL *stop) {
105110
@try {
@@ -175,7 +180,8 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
175180
NSString *oldValue = value;
176181

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

180186
// 检查特殊情况
181187
if (decimalValue == NSDecimalNumber.notANumber) {

MJExtensionDemo.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

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

8586
/* Begin PBXFileReference section */
87+
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJFrenchUser.h; sourceTree = "<group>"; };
88+
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJFrenchUser.m; sourceTree = "<group>"; };
8689
2D2DB9C82317DA07005A689E /* MJExtensionDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MJExtensionDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
8790
2D2DB9CB2317DA07005A689E /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
8891
2D2DB9CC2317DA07005A689E /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
@@ -275,6 +278,8 @@
275278
2D2DBA752317DBDF005A689E /* MJExtensionConfig.m */,
276279
2D2DBA762317DBDF005A689E /* MJBook.h */,
277280
2D2DBA772317DBDF005A689E /* MJPerson.m */,
281+
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */,
282+
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */,
278283
);
279284
path = Model;
280285
sourceTree = "<group>";
@@ -463,6 +468,7 @@
463468
2D2DBA822317DBE0005A689E /* MJStatus.m in Sources */,
464469
2D2DBA832317DBE0005A689E /* MJAd.m in Sources */,
465470
2D2DBA802317DBE0005A689E /* MJUser.m in Sources */,
471+
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */,
466472
2D2DBA852317DBE0005A689E /* MJPerson.m in Sources */,
467473
2D2DBA7B2317DBE0005A689E /* MJBox.m in Sources */,
468474
2D2DBA582317DBB9005A689E /* MJExtensionTests.m in Sources */,

MJExtensionTests/MJExtensionTests.m

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "MJBook.h"
1818
#import "MJBox.h"
1919
#import <CoreData/CoreData.h>
20+
#import "MJFrenchUser.h"
2021

2122
@interface MJExtensionTests : XCTestCase
2223

@@ -65,24 +66,24 @@ - (void)testJSON2Model {
6566
XCTAssert(user.collect == 40);
6667
XCTAssert(!user.alien);
6768
}
68-
69+
// 特殊模型, 数字为法语逗号为小数点的分隔符情况
6970
- (void)testJSON2NumberModel {
7071
// 1.定义一个字典
7172
NSDictionary *dict = @{
7273
@"age" : @"20",
7374
@"height" : @1.55,
74-
@"money" : @"100.9",
75+
@"money" : @"100,9",
7576
@"gay" : @"",
76-
@"speed" : @"120.5",
77+
@"speed" : @"120,5",
7778
@"identifier" : @"3443623624362",
78-
@"price" : @"20.3",
79+
@"price" : @"20,3",
7980
@"like" : @"20个",
8081
@"collect" : @"收藏5",
8182
@"rich" : @"hehe",
8283
};
8384

84-
// 2.将字典转为MJUser模型
85-
MJUser *user = [MJUser mj_objectWithKeyValues:dict];
85+
// 2.将字典转为MJFrenchUser模型
86+
MJFrenchUser *user = [MJFrenchUser mj_objectWithKeyValues:dict];
8687

8788
XCTAssert(user.age == 20);
8889
XCTAssert(user.height.doubleValue == 1.55);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// MJFrenchUser.h
3+
// MJExtensionTests
4+
//
5+
// Created by Frank on 2019/9/26.
6+
// Copyright © 2019 MJ Lee. All rights reserved.
7+
//
8+
9+
#import "MJUser.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface MJFrenchUser : MJUser
14+
15+
@end
16+
17+
NS_ASSUME_NONNULL_END
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// MJFrenchUser.m
3+
// MJExtensionTests
4+
//
5+
// Created by Frank on 2019/9/26.
6+
// Copyright © 2019 MJ Lee. All rights reserved.
7+
//
8+
9+
#import "MJFrenchUser.h"
10+
11+
@implementation MJFrenchUser
12+
13+
+ (NSLocale *)mj_numberLocale {
14+
return [NSLocale localeWithLocaleIdentifier:@"fr_FR"];
15+
}
16+
17+
@end

0 commit comments

Comments
 (0)