【问题标题】:SwiftUI "Generic parameter 'Data' could not be inferred" when referencing variables from within ForEach从 ForEach 中引用变量时,SwiftUI“无法推断通用参数'数据'”
【发布时间】:2019-11-17 21:52:02
【问题描述】:

因此,由于某种原因,当我尝试在 ForEach 中引用变量时,它给了我以下错误:无法推断通用参数“数据”,明确指定通用参数来解决此问题”

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

    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
    }

}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      更正:@State var optionHeight : CGFloat = 50

      -> 编译器假设 optionHeight 是一个 Int,如果你不告诉他 ;)

      【讨论】:

        猜你喜欢
        • 2022-08-17
        • 2020-04-26
        • 2020-05-26
        • 2019-12-16
        • 1970-01-01
        • 2020-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多