我的好奇心使我将View
类型作为参数传递给@ViewBuilder
. 将模型/原始类型作为参数传递@ViewBuilder
是完全有效的。
如下代码所示。
struct TestView<Content: View>: View {
let content: (String) -> Content
init(@ViewBuilder content: @escaping (String) -> Content) {
self.content = content
}
var body: some View {
content("Some text")
}
}
struct ContentTestView: View {
var body: some View {
TestView {
Text("\($0)")
}
}
}
代替String
在
let content: (String) -> Content
如果我尝试传递 SwiftUIView
类型,那么 Compiler 会对此不满意。
let content: (View) -> Content
即使 params for@ViewBuilder
接受自定义协议类型,例如Searchable
但不接受View
协议。
编译器告诉我这个Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
我的整个想法是content
可以允许保留Section/List/Text
它。
编辑:我希望代码如下。
struct TestView<Content: View>: View {
let content: (View) -> Content
init(@ViewBuilder content: @escaping (View) -> Content) {
self.content = content
}
var body: some View {
content(
List {
ForEach(0..<10) { i in
Text(\(i))
}
}
)
}
}
struct ContentTestView: View {
var body: some View {
TestView { viewbody -> _ in
Section(header: Text("Header goes here")) {
viewbody
}
}
}
}
我有什么办法可以做到这一点?