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>?. (Do not worry about the strange syntax of the measurement type; it is a generic type, and you will be learning more about them later.)
Listing 6.5 Adding a variable to store the temperature (ConversionViewController.swift
)
@IBOutlet var celsiusLabel: UILabel! @IBOutlet var textField: UITextField! var fahrenheitValue: ...
Get iOS Programming: The Big Nerd Ranch Guide, 7th 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.