In SwiftUI, a `List` will automatically format the sub views you pass it, so something like this:
List {
Text("A")
Text("B")
}Will result in both text views being correctly placed, with separators between them etc...
Additionally, it is also possible to mix static and dynamically generated data, like this:
List {
Text("A")
ForEach(foo) { fooElement in CustomView(fooElement) }
Text("B")
ForEach(bar) { barElement in CustomView(barElement) }
}My goal is to write my own custom type that would allow this kind of use by its users (ie: a view that lets the users provide views using the new function builder DSL, without requiring them to write their own modifiers to place them on the screen), but I don't know at all what to put in the initialiser of my custom view.
The native SwiftUI views are making use of `@ViewBuilder` and receive a generic type conforming to `View`, but the way they are able to extract elements (from, say, a `ForEach` view) is mysterious and I'm pretty sure it's not even possible.
An example might be clearer. Many of you must have seen the nice examples online with cards arranged on top of each other using a `ZSack`, so what if I want to create a `CardStack` type that would allow my users to write code like this?
CardStack {
SomeView()
AnotherView()
ForEach(1...10) { i in
NumberView(i)
}
}This would result in 12 cards stacked on top of each other, note that the types are not homogenous and that we used `ForEach`.
Maybe I missed something, so I'm curious to know your opinion about this?
Thank you