UIViewRepresentable code does not work - Chapter 4

The UIViewRepresentable code does not work as outlined in Chapter 4. The binding give all sorts of errors. This is how I solved the problem and it is now working. I reference the ColorUISlider in the Coordinator by way of a parent variable and then I can assign the slider value to the bound value variable.

struct ColorUISlider: UIViewRepresentable {
var color: UIColor
@Binding var value:Double

class Coordinator: NSObject {
    let parent:ColorUISlider
    init(parent:ColorUISlider) {
        self.parent = parent
    @objc func valueChanged(_ sender: UISlider) {
        self.parent.value = Double(sender.value)

func makeCoordinator() -> ColorUISlider.Coordinator {
    Coordinator(parent: self)

func makeUIView(context: Context) -> UISlider {
    let slider = UISlider(frame: .zero)
    slider.thumbTintColor = color
    slider.value = Float(value)
    slider.addTarget(context.coordinator, action: #selector(Coordinator.valueChanged(_:)), for: .valueChanged)
    return slider

func updateUIView(_ uiView: UISlider, context: Context) {
    uiView.value = Float(value)


struct ColorUISlider_Previews: PreviewProvider {
    static var previews: some View {
        ColorUISlider(color: .red, value: .constant(0.5))

I am getting this error for the same example

Use my code. It solves the problem.
Something must have changed between betas and the authors have not yet caught up to it.

1 Like

Also self.value.wrappedValue solves the issue too, May be you are right… Its still in early access.


If you use .wrappedValue then you shouldn’t have to change any of the other code. Worked for me at least.


this is exactly what I did