As you can see when you attempt to add your currently-commented second sink
, you’ll end up with a circular dependency between the inches and centimeters. Instead, I’d suggest you store one value and use a custom binding for the other:
struct Centimeters {
var value: Double
}
class SizeValueModel: ObservableObject {
@Published var centimeters: Centimeters
var inchesBinding : Binding<Double> {
.init {
self.centimeters.value / 2.54
} set: {
self.centimeters.value = $0 * 2.54
}
}
init() {
self.centimeters = Centimeters(value: 1.0)
}
}
struct ContentView: View {
@StateObject var model = SizeValueModel()
var body: some View {
Slider(value: $model.centimeters.value, in: 0...100, label: {
Text("\(model.centimeters.value)")
})
Slider(value: model.inchesBinding, in: 0...39.3701, label: {
Text("\(model.inchesBinding.wrappedValue)")
})
}
}
CLICK HERE to find out more related problems solutions.