Open olegivo opened 1 year ago
You can support this in SwiftUI with generics. Something like this (apologies if this doesn't compile, writhing this code directly in a comment):
struct ItemView<Model>: View where Model: ItemViewModel & ViewModel {
@ObservedObject var viewModel: Model
var body: some View {
Text(viewModel.state(\.title))
}
}
struct ItemView_Previews: PreviewProvider {
static var previews: some View {
ItemView<ItemViewModelFake>(
viewModel: ItemViewModelFake()
)
}
}
Interfaces are useful in case of faking in previews. For example:
And use them for previews:
But not for SwiftUI. We can't make extensions for protocols (to be ObservableObject).