swiftuiinitializationtemperature

Declaring a temperature type variable in SwiftUI


I want to create a universal converter for temperature, distance and time. In each of the sub-main views the user should be able to choose the units they want to convert and those which they want to receive. I struggle with initializing default values for the input though, getting an error:

Type 'Unit' has no member 'celsius'

The code:

import SwiftUI
import Combine

private final class TempConverterViewState: ObservableObject {
     @Published var temperatureInput = Measurement(value: 0, unit: .celsius) }

Can you help me initialize this variable, please or point me to some further reading?


Solution

  • temperatureInput = Measurement(value: 0, unit: UnitTemperature.celsius)