SwiftUI ForEach 无法推断复杂闭包 return 类型;添加显式类型以消除歧义
SwiftUI ForEach Unable to infer complex closure return type; add explicit type to disambiguate
我的代码很简单:
struct ListView: View {
var body: some View {
ScrollView(.vertical) {
VStack(alignment: .leading) {
ForEach(0...10, id: \.self) { _ in
CellView()
Spacer()
}
}
}
}
}
struct CellView: View {
var body: some View {
Text("Template Text Template Text Template Text Template Text ")
}
}
但是显示错误:
如果我评论Spacer() or CellView()
,效果很好
另一方面,我在不同的地方有另一个代码可以很好地编译:
为什么会发生?
ForEach
里面应该是单视图,所以把它们包在一些堆栈里,比如
ForEach(0...10, id: \.self) { _ in
HStack {
CellView()
Spacer()
}
}
我的代码很简单:
struct ListView: View {
var body: some View {
ScrollView(.vertical) {
VStack(alignment: .leading) {
ForEach(0...10, id: \.self) { _ in
CellView()
Spacer()
}
}
}
}
}
struct CellView: View {
var body: some View {
Text("Template Text Template Text Template Text Template Text ")
}
}
但是显示错误:
如果我评论Spacer() or CellView()
,效果很好
另一方面,我在不同的地方有另一个代码可以很好地编译:
为什么会发生?
ForEach
里面应该是单视图,所以把它们包在一些堆栈里,比如
ForEach(0...10, id: \.self) { _ in
HStack {
CellView()
Spacer()
}
}