Converting two values of type Binding<Double> to Binding<CGPoint>

Viewed 136

I'd like to assign two values of type Binding<Double> to a CGPoint which I'd also like bound to my UI, but I am not having any luck with the following:

public init(valueX: Binding<Double>, valueY: Binding<Double>){
    self._value = Binding<CGPoint>(x: valueX, y: valueY) /// Error: No exact matches in call to initializer
}

How should I go about doing this?

1 Answers

Here is a solution. Tested with Xcode 12.1 / iOS 14.1

public init(valueX: Binding<Double>, valueY: Binding<Double>){
    self._value = Binding<CGPoint>(
        get: { CGPoint(x: valueX.wrappedValue, y: valueY.wrappedValue) },
        set: { valueX.wrappedValue = Double($0.x); valueY.wrappedValue = Double($0.y)})
}
Related