Context
I am working with Generic SwiftUI Views
and encountered a problem. I have a Generic SwiftUI View
and when calling it, I need to specify the Type
. However, I get this Type
only as a return parameter from a method in an any
format.
The following Code produces the following Compiler Error:
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
Cannot find type 'componentType' in scope
Code
protocol Component {
var name: String { get }
}
struct GenericView<C: Component>: View {
var component: C?
var body: some View { Text(component.name) }
}
struct MainView: View {
var body: some View {
GenericView<componentType>()
}
private var componentType: any Component.Type {
// This returns a specific Component Type, e.g. ComponentA.self
}
}
Question
- How can I achieve my goal of adjusting the
Generic Type
ofGenericView
depending on aParameter
/Method Return Value
?