简体   繁体   English

NSNumberFormatter在设备上返回nil,而不是在模拟器上返回

[英]NSNumberFormatter returns nil on device, not on simulator

I use a NSNumberFormatter to convert a user input to a NSNumber (a decimal number), I use ARC. 我使用NSNumberFormatter将用户输入转换为NSNumber(十进制数),我使用ARC。

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
[f setMaximumFractionDigits:1];

_myNumber = [f numberFromString:myTextField.text];

Sometimes this results in _myNumber to be nil . 有时这会导致_myNumber nil Even when I am absolutely sure the users input is a correct decimal number (I checked during debugging). 即使我绝对确定用户输入的是正确的十进制数(我在调试期间检查过)。

For completeness: _myNumber is a synthesized property of the ViewController. 为了完整性: _myNumber是ViewController的综合属性。 This only happens when running the app on a device, not when I run it in the simulator. 这仅在设备上运行应用程序时发生,而不是在我在模拟器中运行时。 The keyboard being used is the 'Decimal Pad' In a different section of code, in a different ViewController the code does work. 使用的键盘是'Decimal Pad'在不同的代码段中,在不同的ViewController中代码可以正常工作。

I now have a workaround which I have added below the above code: 我现在有一个解决方法,我在上面的代码下面添加了:

if (!myNumber){
    myNumber = [NSNumber numberWithFloat: myTextField.text.floatValue];
}

Does anybody know why NSNumberFormatter can return nil, even when calling [NSNumber numberWithFloat: myTextField.text.floatValue] works? 有人知道为什么NSNumberFormatter可以返回nil,即使调用[NSNumber numberWithFloat: myTextField.text.floatValue]有效吗?

In my case the solution was set the "decimalSeparator" for the NSNumberFormatter to ".", example: 在我的例子中,解决方案是将NSNumberFormatter的“decimalSeparator”设置为“。”,例如:

NSNumberFormatter *formatString = [[NSNumberFormatter alloc] init];
formatString.decimalSeparator = @".";

Why this was working on the simulator and not on the device depended on the Language settings. 为什么这个在模拟器上运行而不是在设备上依赖于语言设置。 My simulator is set to English while my device is set to Dutch. 当我的设备设置为荷兰语时,我的模拟器设置为英语。 When in English a “.” is used as a decimal separator, when in Dutch a “,” is used. 当用英语时,“。”用作小数分隔符,当用荷兰语时,使用“,”。

When writing the original value to the UITextField I used the following code: 将原始值写入UITextField时,我使用了以下代码:

_myTextField.text = [NSString stringWithFormat:@”%.1f”, [_myNumber floatValue]];

This results in always having the “.” as a decimal separator which may or may not be correct depending on the Language settings. 这导致始终将“。”作为小数分隔符,根据语言设置可能会或可能不正确。 What I had to do was to use a NSNumberFormatter to set the value into the UITextField: 我必须做的是使用NSNumberFormatter将值设置为UITextField:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
[f setMaximumFractionDigits:1];

_myTextField.text = [f stringFromNumber:_myNumber];

I had the same Problem but not with Text input but with consuming a API where the decimal separator is ".". 我有相同的问题,但不是文本输入,但消耗了一个小数分隔符为“。”的API。 Using an DecimalNumberFormatter fails on my Devices (Germany uses ",") but not on the Simulator (which may uses the US Region setting). 在我的设备上使用DecimalNumberFormatter失败(德国使用“,”)但在模拟器上没有(可能使用美国区域设置)。

My fix is to determine the decimal separator and providing it to the formatter object: 我的修复是确定小数分隔符并将其提供给formatter对象:

    f.decimalSeparator = "."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSNumberFormatter在设备和模拟器上有所不同 - NSNumberFormatter different on device and simulator NSnumberFormatter在模拟器上工作,但不在设备上工作 - NSnumberFormatter working on simulator but not on device NSNumberFormatter.numberFromString()为ar-SA语言环境返回nil - NSNumberFormatter.numberFromString() returns nil for ar-SA locale DateFormatter在iPhone上返回nil,但在模拟器上不返回nil - DateFormatter returns nil on iPhone, but not on simulator 为什么FBSDKAccessToken.currentAccessToken()在我的设备上返回nil,但是在iOS模拟器中可以正常工作? - Why does FBSDKAccessToken.currentAccessToken() returns nil on my device but works fine in the iOS simulator? UILabel在运行时为nil,可在模拟器上运行,但在设备上崩溃 - UILabel is nil on runtime, works on simulator, but crashes on device device.newDefaultLibrary()返回nil - device.newDefaultLibrary() returns nil CFBundleCopyResourceURL在设备中返回NULL,但在模拟器上不返回 - CFBundleCopyResourceURL returns NULL in device, but not on simulator NSNumberFormatter numberFromString返回null - NSNumberFormatter numberFromString returns null 请求在模拟器与设备上返回不同的数据 - Request returns different data on simulator vs device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM