Implementing the Temperature Conversion

With the basics of the interface wired up, let’s implement the conversion from Fahrenheit to Celsius. You are going to store the current Fahrenheit value and compute the Celsius value whenever the text field changes.

In ConversionViewController.swift, add a property for the Fahrenheit value. This will be an optional double (a Double?).

@IBOutlet var celsiusLabel: UILabel!

var fahrenheitValue: Double?

The reason this property is optional is because the user might not have typed in a number, similar to the empty string issue you fixed earlier.

Now add a computed property for the Celsius value. This value will be computed based on the Fahrenheit value.

var fahrenheitValue: Double?

var celsiusValue: ...

Get iOS Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.