我自定义一个结构体,然后有个函数想传递一个@ViewBuilder包装的闭包,我是想把多个视图组成的闭包作为参数传进去,但外部调用的时候会报错
struct CustomView<T: View> {
func test(@ViewBuilder content : () -> T) {
print(content())
}
}
上面的定义没问题,然后调用的时候如果传递多行Text编译器会报错Cannot convert value of type 'TupleView<(Text, Text)>' to closure result type 'Text',我的理解是@ViewBuilder根本没有生效,传递一行Text是没问题的,然后查了下网上好像@ViewBuilder都是用在init上或者修饰函数才行,难道真不能用在普通函数的参数上?
struct TestView: View {
let view = CustomView<Text>()
var body: some View {
VStack {
Button(action: {
/// Cannot convert value of type 'TupleView<(Text, Text)>' to closure result type 'Text'
view.test() {
Text("1111")
Text("2222")
}
}){}
}
}
}