How to publish changes to subscribers, when an ObservableObject property that is delegated changes?

I'm looking at use case where I'd need to publish changes to subscribers when an ObservableObject property that is delegated(*) changes, as follows:

struct ContentView: View {
    @EnvironmentObject var foobar: Foobar

    var body: some View {
        VStack {
            Text("localProp1 is \(String(format: "%.2f", self.foobar.someInstance.prop1))")
            Slider(value: self.$foobar.someInstance.prop1)
        }
    }
}

class Foobar: ObservableObject {
  var someInstance: SomeInstance = SomeInstance()
}

I don't have control over SomeInstance, this is a third-party library and I would have to use the @Published keyword before the property so that can store and publish their changes to the subscriber, correct?

(*)I hope I explain this well, is my understanding that a Delegate is an instance of an object assigned to an object property, correct me otherwise

You may find this thread helpful:

1 Like

Ok, thanks @Lantua

I don't think that's correct. The word delegate is used with a certain design pattern. It's common in UIKit and some other Apple libraries. A typical example is UITableView and UITableViewDelegate. You implement the delegate and connect it to the table view as a way to customize that table view. The table view will call its delegate for various things.

For what you have, I'd call that simply an object with a reference to another object.

I don't think there is a way to do what you want with @Published. You might have to manually wire it up...

class Foobar: ObservableObject {
    var someInstance = SomeInstance()
    init() {
        someInstance.onSomeEvent { _ in 
            self.objectWillChange.send()
        }
    }
}

Thanks for the tip regarding "delegate"!