I use NSNumberFormatter to convert user input to NSNumber (decimal), I use ARC.
NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; [f setMaximumFractionDigits:1]; _myNumber = [f numberFromString:myTextField.text];
Sometimes this results in _myNumber as nil . Even when I'm absolutely sure that user input is the correct decimal number (I checked during debugging).
For completeness: _myNumber is a synthesized property of the ViewController. This only happens when the application is launched on the device, and not when it is launched in the simulator. The keyboard used is the Decimal Panel. In another section of code in another ViewController, the code really works.
Now I have a workaround that I added below the code above:
if (!myNumber){ myNumber = [NSNumber numberWithFloat: myTextField.text.floatValue]; }
Does anyone know why NSNumberFormatter can return zero even when [NSNumber numberWithFloat: myTextField.text.floatValue] ?
source share