Right now, I've been running into an issue of registering protocol conforming to ObservableObject. Since ObservableObject uses an associated type, I cannot find a way to register it without compiling error.
This is my simple model, called A
protocol A: ObservableObject {}
This is my simple class, called B
final class B: A {}
Here is my Assembly
final class ViewStoreAssembly: Assembly{
func assemble(container: Container) {
container.register(A.self) { _ in
B()
}
}
}
I'm trying to register A comforming to ObservableObject, but the compilation error shows that
Protocol 'A' can only be used as a generic constraint because it has Self or associated type requirements
Does anyone know how to tackle the issue? The benefits of doing this will be
- In SwiftUI, we can decouple view out of ViewStore/presenter so that view can be separated from viewModel holder
- ViewStore/Presenter can be tested easily by mocking a view