目标
获取数据以显示在 scrollView
预期结果

实际结果

选择
use List,但不灵活(不能去掉分隔符,不能有多个列)
代码
struct Object: Identifiable {
var id: String
}
struct Test: View {
@State var array = [Object]()
var body: some View {
// return VStack { // uncomment this to see that it works perfectly fine
return ScrollView(.vertical) {
ForEach(array) { o in
Text(o.id)
}
}
.onAppear(perform: {
self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
})
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
struct CustomTabView: View where Content: View {
let children: [AnyView]
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
let m = Mirror(reflecting: content())
if let value = m.descendant("value") {
let tupleMirror = Mirror(reflecting: value)
let tupleElements = tupleMirror.children.map({ AnyView($0.value) }) // ERROR
self.children = tupleElements
} else {
self.children = [AnyView]()
}
}
var body: some View {
ForEach(self.children) { child in
child...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将 转换TupleView为数组,AnyView但我收到错误
Protocol type 'Any' …Run Code Online (Sandbox Code Playgroud)