So I am trying to create a router. I intended to route directly to a view in the body such as..
struct MyView: View {
let page: Page
var body: some View {
router(page: page)
}
}
In the body I tried to declare a function...
func router(page: Page) -> View
which gave me the error:
Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
so I then made it
func router<T: View>(page: Page) -> T
then in the body I used a switch statement:
func router<T: View>(page: Page) -> T {
switch page {
case .page1:
return Page1()
....
}
}
then Xcode recommended that I cast all returned views as! T
which ultimately led me to the generic warning against generics as been below:
I search SO for this problem and there are many variants but none led to me understand why this doesn't work.