Search code examples
swiftswiftuicombine

Get value change update on Binding in SwiftUI


I have a binding on my SwiftUI view

@Binding var pinValue: String

I want to get update every time the value is changed. I tried below as I'd do on a publisher but I'm getting errors because it's not a publisher.

.onReceive($pinValue, perform: { output in
            print(output)
        })

I've also tried to access $pinValue.publisher but the .onReceive block wont' work.

How can I get an update every time the value of pinValue is changed?


Solution

  • Use this instead:

    .onChange(of: pinValue) { output in
      print(output)
    }