Coming from a java/kotlin background I am trying to get my head around swift protocols with associated types. Why does the following snippet fail to compile with error: "Type ViewFactoryImpl does not conform to protocol ViewFactory" and what would be the solution?
protocol ViewFactory {
associatedtype V: View
func createSomeView() -> V
func createAnotherView() -> V
}
class ViewFactoryImpl: ViewFactory {
func createSomeView() -> some View {
return ViewA()
}
func createAnotherView() -> some View {
return ViewB()
}
}
By playing around i was able to compile by defining the following instead
protocol ViewFactory {
associatedtype V1: View
associatedtype V2: View
func createSomeView() -> V1
func createAnotherView() -> V2
}
I do not understand the issue. I thought the associated type is defined for the entire protocol and can be used in multiple methods. What I am missing?
The solution you came up with by playing around is exactly what you need
As mentioned elsewhere, the main issue with your first protocol is that you're enforcing createSomeView()
createAnotherView()
both return the same type. While ViewA()
and ViewB()
are both candidates for V
, since they conform to View
they are still different types, and therefore cannot BOTH be V
in a given object.
By defining both V1
and V2
, you allow for each function to return a different type, or the same type, it's all acceptable. By making both V1
and V2
require View
conformance, you allow for the some View
syntax