简体   繁体   中英

How can I access to views of TupleView in SwiftUI?

Here is a simple way to show what I am trying to do:

let test = ("Hello", " world!")

let read = test.0

As you can see I defined a tuple and in next line I accessed item 0 of it. I am trying to do the same thing with TupleView like this:

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!
            
        }
        
    }
    
}

I am getting this error from xCode:

Value of type '() -> TupleView<(Content1, Content2)>' has no member '0'

Value of type '() -> TupleView<(Content1, Content2)>' has no member '1'

What I should do for reach to my goal of reading item 0 and item 1 in body? with usage of TupleView

Here is the answer with help of 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)
            
        }
        
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM