从 ForEach 中引用变量时的 SwiftUI "Generic parameter 'Data' could not be inferred"



因此,出于某种原因,当我尝试引用ForEach中的变量时,它会给我以下错误:无法推断泛型参数"Data",显式指定泛型参数以解决此问题">

我真的不知道为什么这是一个错误,这对我来说似乎很简单,但我卡住了

struct OptionsScrollView: View {
@State var scrollOptions = [
"test1",
"test2",
"test3"
]
@State var optionHeight = 50
var body: some View {
GeometryReader{outerGeo in
ScrollView{
VStack{
ForEach(self.scrollOptions.indices) {i in
GeometryReader {optionGeo in
Text( self.scrollOptions[i] )
} // end optionGeo
.frame(width: 100, height: self.optionHeight)
//                         ^^^^^^^^^^^^^^^^^
//                         adding this gives me the error
} // end ForEach
} // end VStack
} // end ScrollView
} // end outerGeo
}
}

删除ScrollView上方的GeometryReader解决了我的问题,不知道为什么!

更正:@State var optionHeight:CGFoat=50

->编译器假设optionHeight是Int,若你们并没有告诉他;(

最新更新