Math calculator crashed in some language/regional settings

Hi guys,


I just started coding a couple years ago and couldn't find an answer to the following issue yet. The math calculator built works fine in EnglishUS/USA, EnglishCanada/Canada, EnglishUK/UK, EnglishAustralia/Astralia and other regions. However, it crashed if phone set for some european countries. Xcode Decimal pad is used for the calculator. I believe that the failure has to do with dot (.) or commas (,) used in many countries as decimal separtor. That's why I had included NumberFormatter method in my code from the very beginning. It still didn't resolve the issue. I assume I didn't apply NumberFormatter correctly. When the app crashes, it specifies the following error: "Can't find keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad; using 20628_Portrait_iPhone-Simple-Pad_Default".


The code is as following bellow. Any help is appriciated.

import UIKit


class A_ViewController: UIViewController {


@IBOutlet weak var originalMWTextField: UITextField!

@IBOutlet weak var shutIDPPtextField: UITextField!

@IBOutlet weak var trueVDtextField: UITextField!

@IBOutlet weak var switchTapped: UISwitch!

@IBOutlet weak var outputTextField: UITextField!

override func viewDidLoad() {

super.viewDidLoad()


// Do any additional setup after loading the view.

}


@IBAction func calculateTappedButton(_ sender: Any) {



let num = NumberFormatter()

let firstValue = Double(num.number(from:originalMWTextField.text!)!)

let secondValue = Double(num.number(from:shutIDPPtextField.text!)!)

let thirdValue = Double(num.number(from:trueVDtextField.text!)!)

if switchTapped.isOn {

let outputValue = secondValue * 19.23/thirdValue + firstValue

outputTextField.text = String(format:"%.2f ppg. Round up if required!", outputValue)

} else {

let outputValue = secondValue * 19.23/thirdValue * 3.281 + firstValue

outputTextField.text = String(format:"%.2f ppg. Round up if required!", outputValue)

}

}

@IBAction func gestureTapped(_ sender: Any) {

view.endEditing(true)

}

}

Accepted Answer

The problem is not the backplane (I find those messages useless), but the Locale you use.


Where does it crash exactly ? Is it line 20 ?


import UIKit

class A_ViewController: UIViewController {

    @IBOutlet weak var originalMWTextField: UITextField!
    @IBOutlet weak var shutIDPPtextField: UITextField!
    @IBOutlet weak var trueVDtextField: UITextField!
    @IBOutlet weak var switchTapped: UISwitch!
    @IBOutlet weak var outputTextField: UITextField!
  
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
  
    @IBAction func calculateTappedButton(_ sender: Any) {

        let num = NumberFormatter()
        let firstValue = Double(num.number(from:originalMWTextField.text!)!)
        let secondValue = Double(num.number(from:shutIDPPtextField.text!)!)
        let thirdValue = Double(num.number(from:trueVDtextField.text!)!)
      
        if switchTapped.isOn {
            let outputValue = secondValue * 19.23/thirdValue + firstValue
          
            outputTextField.text = String(format:"%.2f ppg. Round up if required!", outputValue)
          
        } else {
            let outputValue = secondValue * 19.23/thirdValue * 3.281 + firstValue
          
             outputTextField.text = String(format:"%.2f ppg. Round up if required!", outputValue)
        }
        }
    
    @IBAction func gestureTapped(_ sender: Any) {
        view.endEditing(true)
    }
}



Clearly, you have locale as "en" ; when you type 1,2 (not 1.2) then you crash on line 20


You could have a test to check for comma or dot (it is not very clean though !)




let num = NumberFormatter()
print(num.locale!)     // to test locale on iPhone
var value = originalMWTextField.text!
if num.number(from:value) == nil {
    value = value.replacingOccurrences(of: ".", with: ",")
     if num.number(from:value) == nil {     // Test with comma
        value = "0"     // not a number at the end
     }
}


let firstValue = Double(truncating: num.number(from:value)!)
print(firstValue)

Yes Claude31, it was crashing in line 20 as you said.


Looks like the block of test code you proposed works just fine to prevent the failure and it still works in USA/UK regions as well. Thanks a lot for your help!

Math calculator crashed in some language/regional settings
 
 
Q