A ViewModel
class has a sourceProperty
that is being edited by the TextField
. That property is @Published
. I'd like to pass it to the Logic
class which has an initializer with Binding<String>
. That class will be listening to the sourceProperty
changes, react on them and set it's output to the @Published output
property.
How can I pass @Published sourceProperty
as an initializer parameter to the Logic
class?
Relevant code:
final class ViewModel {
@Published var sourceProperty: String = ""
private var logic: Logic?
init() {
self.logic = Logic(data: $sourceProperty)
$logic.output.sink({result in
print("got result: \(result)")
})
}
}
final class Logic: ObservableObject {
private var bag = Set<AnyCancellable>()
@Published var output: String = ""
@Binding var data: String
init(data: Binding<String>) {
self._data = data
$data.sink({ newValue in
output = newvalue + "ABCDE"
}).store(in: &bag)
}
}
So far I'm getting the following error:
Cannot convert value of type 'Published.Publisher' to expected argument type 'Binding'
The goal is to use a change in the object's own property to trigger a method invocation in another object and then bind that second object's output to some view.
View Layer:
public struct ViewLayer: View {
@Binding private var sourceProperty: String
public init(_ placeholder: String,
sourceProperty: Binding<String>,
) {
self.placeholder = placeholder
self._sourceProperty = sourceProperty
}
public var body: some View {
TextField(placeholder, text: $sourceProperty)
}
}
@Binding
should only be used inside `Views. It would be easier to help if your question included the view layer as well. – BragaBinding
? You're trying to usesink
on it, which is what comes from aPublisher
– Ingamarbinding
. What I'd like to achieve is to connect two classes together, in a way that when asourceproperty
gets written to (by the view),Logic
class will react on that change and update it's own properties. – ZebadiahLogic
. But, I'm struggling to see why you need nestedObservableObject
s as well. That seems suspicious. I get why yourViewModel
would be anObservableObject
, but not whyLogic
would. And right now, it's the reverse in your code. – IngamarViewModel
both anObservableObject
? Neither is really addressed by the view layer that got added. – IngamarObservableObject
from one of the classes. – Zebadiah