struct ModelDemo {
let callback : () -> ()
}
final class ViewModelDemo {
let modelDemo: ModelDemo
init() {
modelDemo = ModelDemo(callback: self.modelCallback)
}
private func modelCallback() {
}
}
The above generates the error "'self' used before all stored properties are initialized."
I understand the error, but this is a common programming pattern for me in other languages. Is there a preferred way to do something like this in Swift?
My current workaround is to make the callback property of ModelDemo into an optional and initialize it to nil, then to set it from the ViewModelDemo init() after it (and other ViewModel properties) have been initialized.
FWIW, the intent of the code is to give the Model a way to inform the ViewModel that something in the Model has changed. Since the Model is supposed to be isolated from the View, I don't think I should use @ObservableObject as that's a SwiftUI feature.