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 measurement for temperature (a Measurement<UnitTemperature>?).
@IBOutlet var celsiusLabel: UILabel! var fahrenheitValue: Measurement<UnitTemperature>?
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 ...
Get iOS Programming: The Big Nerd Ranch Guide, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.