Dani Turc Dani Turc - 6 months ago 25
Swift Question

Lose some if/else if statments! What should I use to replace them ? - SWIFT -

I'm making a unit convertor, I'm using an array like this :

private var subContentArray = [["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"],
["Milliliter", "Centiliter", "Liter", "Gallon", "Quart", "Pint", "Fluid ounce"],
["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"]]


And I'm using a picker that is set with 3 piker fields :

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if pickerView.tag == 0 {
currentSelection = row
self.pickerGeneral.text = self.pickerGeneral1[row]
self.view.endEditing(true)
} else if pickerView.tag == 1 {
self.pickerTextField.text = self.pickerSubContent[currentSelection][row]
self.view.endEditing(true)
} else if pickerView.tag == 2 {
self.pickedTextField2.text = self.pickerSubContent[currentSelection][row]
self.view.endEditing(true)
}
}


I wanna change the 2nd and 3rd picker by the General picker. So far everything is good, but then I wanna make the calculations and I was trying to do something like this :

func updateOutput(){
//var res: Double
var result: Double
if input.text!.isEmpty {
return

} else if pickerTextField.text! == "Centimeter" && pickedTextField2.text == "Mile" {
result = (Double(self.input.text!)! / 160934.4)
self.output.text! = String("\(result)")

} else if pickerTextField.text! == "Mile" && pickedTextField2.text! == "Centimeter" {
result = (Double(self.input.text!)! * 160934.4)
self.output.text! = String("\(result)")

} else if pickerTextField.text! == "Centimeter" && pickedTextField2.text == "Millimeter" {
result = (Double(self.input.text!)! * 10)
self.output.text! = String("\(result)")

} else if pickerTextField.text! == "Millimeter" && pickedTextField2.text! == "Centimeter" {
result = (Double(self.input.text!)! / 10)
self.output.text! = String("\(result)")

}


...until I realize that if I'm gonna keep writing if/else if...i won't end soon and it will look really messy!

I'm still a noob in to OOP and this branch but willing to learn...so, any help, with example and explains will be very well appreciated!!!!

Answer

You can use a dictionary:

let factors : [(String, String): Double] = [("Centimeter", "Mile"): 1.0/160934.4, 
                                            ("Mile", "Centimeter"): 160934.4, 
                                            <and more...>]

and your function becomes

func updateOutput(){
    if input.text!.isEmpty {
        return
    }
    let result = (Double(self.input.text!)! * factors[(pickerTextField.text, pickerTextField2.text)]  
    self.output.text! = String("\(result)")
}

(Warning: completely untested.)

Using a common base unit would be even better.

let in_meters: [String: Double] = ["Centimeter": 0.01,
                                   "Meter": 1.0,
                                   "Mile": 1609.344,
                                   <and so on...>]

func updateOutput(){
    if input.text!.isEmpty {
        return
    }
    let meters = (Double(self.input.text!)! * in_meters[pickerTextField.text]
    let result =  meters * 1.0 / in_meters[pickerTextField2.text]  
    self.output.text! = String("\(result)")
}
Comments