Hi there, I am trying to write part of a simple code to return either Celcius or Fahrenheit using a segemented control to switch between the two. If I enter a value into the textbox and switch the segmented control, it works great. However, when I try and switch back to the original case, it acts as if there is no text in the textfield and returns to the default value, 0.0. Essentially, I'd like it to check the textfield for text, not just require new text to be entered. Thanks in advance.
Here's the code:
@IBAction func tempConvert(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
temperature = Float(temperatureField.text!) ?? 0.0
let celsius = Measurement(value: (Double(100*((temperature - 32) / 1.8).rounded() / 100)), unit:UnitTemperature.celsius)
print(celsius)
temperatureField.text = String(describing: celsius)
checkTempUnits = true
case 1:
temperature = Float(temperatureField.text!) ?? 0.0
let fahrenheit = Measurement(value: (Double(100*((temperature * 1.8) + 32).rounded()/100)), unit: UnitTemperature.fahrenheit)
temperatureField.text = String(describing: fahrenheit)
checkTempUnits = false
default:
speed = Float(331 + (0.6 * temperature))
}
}