[英]How can I feed ForEach with items of TupleView in SwiftUI?
[英]How can I access to views of TupleView in SwiftUI?
這是一個簡單的方法來展示我正在嘗試做的事情:
let test = ("Hello", " world!")
let read = test.0
如您所見,我定義了一個元組,並在下一行訪問了它的第 0 項。 我試圖用 TupleView 做同樣的事情,如下所示:
struct ContentView: View {
var body: some View {
CustomView {
Text("Hello, world!")
Text("Hello, world!")
}
}
}
struct CustomView<Content1: View, Content2: View>: View {
@ViewBuilder let content: () -> TupleView<(Content1, Content2)>
var body: some View {
return VStack {
content.0.background(Color.red) // <<: here!
content.1.background(Color.yellow) // <<: here!
}
}
}
我從 xCode 收到此錯誤:
'() -> TupleView<(Content1, Content2)>' 類型的值沒有成員 '0'
'() -> TupleView<(Content1, Content2)>' 類型的值沒有成員 '1'
我應該怎么做才能達到閱讀正文中的第 0 項和第 1 項的目標? 使用 TupleView
這是在 Asperi 的幫助下的答案:
struct CustomView<Content1: View, Content2: View>: View {
@ViewBuilder let content: () -> TupleView<(Content1, Content2)>
var body: some View {
return VStack {
content().value.0.background(Color.red)
content().value.0.background(Color.yellow)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.