Home > Software engineering >  Populating a SwiftUI view with output from a combine publisher
Populating a SwiftUI view with output from a combine publisher

Time:02-20

I have looked for ages for an answer to this and it's so tricky to find a direct and clear answer to the problem I'm having.

How do I get the data returned from a combine publisher, to a view?

enter image description here

There are literally thousands of examples of this that show sink -> print. There is nothing that clearly shows how to do anything other than print.

CodePudding user response:

It turns out assign(to:) only works for publishers that return a single value, so you need to ignore the error one way or another. You can add .assertNoFailure() or replaceError(with:).

I suppose if we wanted to handle the error, then we would need to put this into an object with a sink and then assign some local properties with the output.

CodePudding user response:

@State var text = ""
var some View{
   Text(text)
    .padding()
    .onAppear(perform: {
        client.sink(receiveValue:{ value in 
        text = value
    }
  })
}
  • Related