SOLVED: I guess this code is silly, but how to make it smarter? ðŸ¤£ #Bindings #UnitConversion

 Feb '22 Ok, very simple: Three TextFields with Celsius, Fahrenheit and Kelvin. If you change one, the others follow. I'd like to discuss, how to code this smarter. Please join the discussion, if you like! Problems: The values are calculated several times so many Bindings ``````import SwiftUI enum UnitSymbol: String { case celsius = "Â° C" case fahrenheit = "Â° f" case kelvin = "K" } struct ContentView: View { @State private var celsius = Measurement(value: 0, unit: UnitTemperature.celsius) @State private var fahrenheit = Measurement(value: 0, unit: UnitTemperature.fahrenheit) @State private var kelvin = Measurement(value: 0, unit: UnitTemperature.kelvin) var body: some View { Form { BoxView(symbol: .celsius, celsius: \$celsius, fahrenheit: \$fahrenheit, kelvin: \$kelvin) BoxView(symbol: .fahrenheit, celsius: \$celsius, fahrenheit: \$fahrenheit, kelvin: \$kelvin) BoxView(symbol: .kelvin, celsius: \$celsius, fahrenheit: \$fahrenheit, kelvin: \$kelvin) } .ignoresSafeArea(.keyboard) } } struct BoxView: View { var symbol: UnitSymbol @Binding var celsius: Measurement @Binding var fahrenheit: Measurement @Binding var kelvin: Measurement var body: some View { HStack { TextField("Enter value:", value: symbol == .celsius ? \$celsius.value : symbol == .fahrenheit ? \$fahrenheit.value : \$kelvin.value, format: .number) .font(.largeTitle) .keyboardType(.decimalPad) .onChange(of: celsius) { _ in fahrenheit = celsius.converted(to: .fahrenheit) kelvin = celsius.converted(to: .kelvin) } .onChange(of: fahrenheit) { _ in celsius = fahrenheit.converted(to: .celsius) kelvin = fahrenheit.converted(to: .kelvin) } .onChange(of: kelvin) { _ in celsius = kelvin.converted(to: .celsius) fahrenheit = kelvin.converted(to: .kelvin) } Text(symbol.rawValue) .font(.largeTitle) .italic() } } }`````` Feb '22 Here's one way to do it. This uses just a single `Measurement` stored in kelvin and converts it as needed for the individual `TextField`s. That way we don't have to manually convert each time one of the `TextField`s changes, but can rely on the `Binding` mechanism to do it all for us. ``````import SwiftUI struct TemperatureView: View { @State private var temperature = Measurement(value: .zero, unit: UnitTemperature.kelvin) //we only need to store this temperature once //we can then convert it to other scales as needed var body: some View { Form { TemperatureBox(unit: .celsius, temperature: \$temperature) TemperatureBox(unit: .fahrenheit, temperature: \$temperature) TemperatureBox(unit: .kelvin, temperature: \$temperature) } .ignoresSafeArea(.keyboard) } } struct TemperatureBox: View { let unit: UnitTemperature @Binding var temperature: Measurement //create an ad hoc Binding to convert the base temperature // to a Double we can hook into the TextField var formattedTemperature: Binding { Binding( get: { temperature.converted(to: unit).value }, set: { newValue in //remember we are storing the temperature as kelvin \$temperature.wrappedValue = Measurement(value: newValue, unit: unit).converted(to: .kelvin) } ) } var body: some View { HStack { TextField("Enter value", value: formattedTemperature, format: .number) .keyboardType(.decimalPad) Text(unit.symbol) .italic() } .font(.largeTitle) } }`````` 1 Feb '22 Wow! Thank you for this elegant solution. While experimenting with your code, I tried to change this line: ``\$temperature.wrappedValue = Measurement(value: newValue, unit: unit).converted(to: .kelvin)`` to ``temperature = Measurement(value: newValue, unit: unit).converted(to: .kelvin)`` It's still working. Where is the difference?

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a \$10 discount!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topicâ€¦

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

You are not logged in

Link copied to your pasteboard.