简体   繁体   English

TextField:无法推断通用参数“主题”

[英]TextField: Generic parameter 'Subject' could not be inferred

I have a form view designed to edit data for an existing item and I'm able to display the Binded (bound?) value without a problem.我有一个表单视图,用于编辑现有项目的数据,并且可以毫无问题地显示绑定(绑定?)值。 The TextField gives me an error of "Generic parameter 'Subject' could not be inferred" TextField 给我一个错误“无法推断通用参数‘主题’”

Here's the file where the problem is occurring.这是发生问题的文件。

import SwiftUI


struct EditMeasurementView: View {

    @Binding var measurementItem: MeasurementItem

    var body: some View {
        Form {
            Text("\(measurementItem.weight)")
            TextField("Weight", text: $measurementItem.weight)
            // TextField("Date", text: $measurementItem.mdate)
        }
    }
}

struct EditMeasurementView_Previews: PreviewProvider {
    static var previews: some View {
        EditMeasurementView(measurementItem:  .constant(MeasurementItem(weight:"99",mdate:"1/1/2001")))
    }
}

The first item in the form is fine and if I click on '$measurementItem' is is bound to the @Binding.表单中的第一项很好,如果我单击“$measurementItem”,则绑定到 @Binding。 The second item is where the error appears and if I click on '$measurementItem' there, it does not indicate is is linked back to the @Binding.第二个项目是出现错误的地方,如果我在那里单击“$measurementItem”,它并不表示已链接回@Binding。

What am I missing?我错过了什么?

Here is some code to duplicate the problem described above:下面是一些代码来复制上述问题:

import SwiftUI

struct MeasurementItem {
    let weight: String
    let mdate: String
}

struct EditMeasurementView: View {
    @State var measurementItem: MeasurementItem

    var body: some View {
        Form {
            Text("\(measurementItem.weight)")
            TextField("Weight", text: $measurementItem.weight)
        }
    }
}

struct EditMeasurementView_Previews: PreviewProvider {
    static var previews: some View {
        EditMeasurementView(measurementItem:  MeasurementItem(weight:"99",mdate:"1/1/2001"))
    }
}

The line TextField("Weight", text: $measurementItem.weight) will generate the error described by the OP.TextField("Weight", text: $measurementItem.weight)将生成 OP 描述的错误。

However the real problem is not with TextField("Weight", text: $measurementItem.weight) itself but that $measurementItem.weight is a constant (ie it is declared with let instead of var ) and because it's a constant the TextField cannot change the value of $measurementItem.weight .然而,真正的问题不在于TextField("Weight", text: $measurementItem.weight)本身,而是$measurementItem.weight是一个常量(即它是用let而不是var声明的)并且因为它是一个常量,所以TextField不能改变$measurementItem.weight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM