Reputation: 4258
I have a textfield where I can write the an decimal-keypad a value like 25.00
This value I save into CoreData (double format) => Double(txtField.text!)!
It works !
On an device, which is set to "Germany", the value Wille be 25,00
But there I will get this error:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
(swift 5)
Where is my mistake? Thanks !
Upvotes: 0
Views: 161
Reputation: 54706
You need to use a NumberFormatter
to parse the input from the text field. NumberFormatter
correctly handles the different decimal separators used in different locales. The Double
initialise accepting a String
on the other hand does not handle decimal separators other than the decimal point.
NumberFormatter
also defaults to the user's Locale
, so you don't need to set its locale
property when parsing user input.
let numberFormatter = NumberFormatter()
if let numericInput = numberFormatter.number(from: txtField.text!)?.doubleValue {
// handle the `Double` value here
}
Upvotes: 1