Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI observed object do action when its property change

I have observed object which is created in view and I want to have function that will occur when the object bool property is changed. I want something similar to .onTapGesture. Is there a way to do it? have function where the body of the function is created outside of that object?

like image 371
K. Ondřej Avatar asked Oct 16 '25 18:10

K. Ondřej


1 Answers

Let's replicate...

class Demo: ObservableObject {
   @Published var value = false
}

struct DemoView: View {
   @ObservedObject var demo = Demo()

   var body: some View {
      Text("Demo") 
        .onReceive(demo.$value) { flag in
            // call here your function
        }
   }
}
like image 90
Asperi Avatar answered Oct 18 '25 11:10

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!