0

I'm designing an app for the company I work for that uses a basic y=mx+c (or y=mx+b if you're from the US) to take 2 calibrated values and 2 input values to output scale and offset values. The app takes a users input from a total of 4 text fields, and then uses a few formulas to output into a text view. My problem is that the input values need to be 'Doubles' for use in the formulas (Divisor, m_FinalScale, m_FinalOffset) whereas the values that the user inputs are 'Strings?' because they are from a text field. How could I go about unwrapping and converting those optional strings to then use?

override func viewDidLoad() {
    super.viewDidLoad()

    Cal1Field.delegate = self
    Cal2Field.delegate = self
    Input1Field.delegate = self
    Input2Field.delegate = self

}

//Actions

@IBAction func CalibrateTapped(_ sender: Any) {

    var Divisor = 0.0, m_FinalScale = 0.0, m_FinalOffset = 0.0

    // this is what the formulas are

    Divisor = Input2Field.text - Input1Field.text
    m_FinalScale = (Cal2Field.text - Cal1Field.text)/Divisor
    m_FinalOffset = Cal1Field.text - (m_FinalScale*Input1Field.text)



}
Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80
Jwood
  • 9
  • 1
  • 4
    Does this answer your question? [Swift - How to convert String to Double](https://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double) – koen May 20 '20 at 11:52

2 Answers2

0

You can use this extension

extension String {
    var convertItToDouble: Double? {
        return Double(self) 
    }
}

then in your method

@IBAction func CalibrateTapped(_ sender: Any) {
    var Divisor = 0.0, m_FinalScale = 0.0, m_FinalOffset = 0.0

    // this is what the formulas are

    if let doubleCal1Field = Cal1Field.text?.convertItToDouble,  
       let doubleCal2Field = Cal2Field.text?.convertItToDouble, 
       let doubleInput1Field = Input1Field.text?.convertItToDouble, 
       let doubleInput2Field = Input2Field.text?.convertItToDouble {

       Divisor = doubleInput2Field - doubleInput1Field
       m_FinalScale = (doubleCal2Field - doubleCal1Field)/Divisor
       m_FinalOffset = doubleCal1Field - (m_FinalScale*doubleInput1Field)
    }
}
koen
  • 4,535
  • 6
  • 38
  • 77
Jawad Ali
  • 11,075
  • 2
  • 25
  • 39
0

You can use flatMap here:

let input1 = input2Field.text.flatMap(Double.init)
let input2 = input2Field.text.flatMap(Double.init)
let cal1 = cal2Field.text.flatMap(Double.init)
let cal2 = cal2Field.text.flatMap(Double.init)

If you want to avoid the code duplication, you can add an extension over UITextField:

extension UITextfield {
    var doubleValue: Double? { text.flatMap(Double.init) }
}

, which you can later use like this:

let input1 = input2Field.doubleValue
let input2 = input2Field.doubleValue
let cal1 = cal2Field.text.doubleValue
let cal2 = cal2Field.text.doubleValue

A couple of notes here:

  1. I renamed the text fields to start with a lowercase letter, to follow the Swift naming conventions.
  2. input1, input2, cal1, cal2 are optionals, it your call what to do if some are nil (e.g. the user entered invalid values).
Cristik
  • 24,833
  • 18
  • 70
  • 97