SwiftUI中@ViewBuilder只能用在init的参数上而不能用在普通函数的参数上吗

我自定义一个结构体,然后有个函数想传递一个@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")
                    }
                }){}
            }
    }
}

 

呵呵,我也不是很会哦

把 T 的定义放到 函数 中。

struct CustomView {
    func test<T: View>(@ViewBuilder content : () -> T) {
        print(content())
    }
}

struct TestView: View {
    let view = CustomView()

    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")
                }
            }){}
        }
    }
}