I can do a static List like
List {
View1()
View2()
}
But how do i make a dynamic list of elements from an array? I tried the followin
You can now use control flow statements directly in @ViewBuilder
blocks, which means the following code is perfectly valid:
struct ContentView: View {
let elements: [Any] = [View1.self, View2.self]
var body: some View {
List {
ForEach(0 ..< elements.count) { index in
if let _ = elements[index] as? View1 {
View1()
} else {
View2()
}
}
}
}
}
In addition to FlowUI.SimpleUITesting.com's answer you can use @ViewBuilder
and avoid AnyView
completely:
@ViewBuilder
func buildView(types: [Any], index: Int) -> some View {
switch types[index].self {
case is View1.Type: View1()
case is View2.Type: View2()
default: EmptyView()
}
}