无法推断通用参数 'Parent'

Generic parameter 'Parent' could not be inferred

我正在处理我的第一个 Swift/SwiftUI 项目(教程除外)并且 运行 我认为是一个常见问题 -- 错误 Generic parameter 'Parent' could not be inferred --当问题实际上出在 List 时,朝向视图的顶部我正在尝试在表单中生成较低的位置。

我正在构建的应用程序是一个简单的发票应用程序:用户填写表单字段并发送发票。一张发票可以有多个行项目,用户一次输入一个,然后附加到字典中,应该显示在表单中。

这是结构顶部和视图开头的相关变量,我希望在这里正确声明行项目的变量,以便根据用户输入修改它们。

*根据@asperi 下面的建议进行编辑。

@State private var lineItems = [LineItem]()
@State private var lineItem = LineItem()

struct LineItem: Codable, Hashable {

    var productSku: String = ""
    var productName: String = ""
    var quantity: Double = 0
    var unitPrice: Double = 0
}

func addLineItem(lineItem: LineItem){
    lineItems.append(lineItem)
}
...   
var body: some View {
       NavigationView {
          Form {
            Section(header: Text("Customer Information")) { <-- error appears here
              TextField("Customer Name", text: $customerName)
              TextField("Customer Email", text: $customerEmail)
          }

这是表单的相关部分,我试图在其中列出所有当前订单项,然后允许用户插入其他订单项。在我添加 List 代码之前我没有收到任何错误,所以我很确定我在那里做错了什么。

Section(header: Text("Items")) {
    List(lineItems, id: \.self) { item in
        LineItemRow(lineItem: item)
        Text(item.productName)
    }
    TextField("Product SKU", text: $productSKU)
    TextField("Poduct Name", text: $productName)
    TextField("Unit Price", text: $unitPrice, formatter: DoubleFormatter())
    Picker("Quantity", selection: $quantity) {
        ForEach(0 ..< 10) {
            Text("\([=12=])")
        }
    }
    Button(action: {
        self.addLineItem(lineItem: LineItem(productSku:$productSKU,
                                            productName:$productName,
                                            quantity:$unitPrice,
                                            unitPrice:$quantity))
        print($lineItems)
    }, label: {
        Text("Add line item")
    })

}

我已经测试了控制台中的按钮功能,它似乎确实正确地附加到词典中,但我也需要它来显示。

我可能在使用 List 时遇到了一些非常基本的错误。有什么建议吗?

作为参考,这里是整个视图:

struct AddInvoiceForm: View {
    @State private var invoiceNumber: String = ""
    @State private var _description: String = ""
    @State private var dueDate: Date = Date()
    @State private var sendImmediately: Bool = true

    @State private var currency = 0
    @State private var paymentType = 0

    @State private var customerName: String = ""
    @State private var customerEmail: String = ""

    @State private var productSKU: String = ""
    @State private var productName: String = ""
    @State private var quantity: Int = 0
    @State private var unitPrice: String = ""

    @State private var lineItems = [LineItem]()
    @State private var lineItem = LineItem()

    struct LineItem: Codable, Hashable {

        var productSku: String = ""
        var productName: String = ""
        var quantity: Double = 0
        var unitPrice: Double = 0
    }

    func addLineItem(lineItem: LineItem){
        lineItems.append(lineItem)
    }

    @State private var totalAmount: Double = 0.0

    static let currencies = ["USD","GBP"]

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Customer Information")) {
                    TextField("Customer Name", text: $customerName)
                    TextField("Customer Email", text: $customerEmail)
                }
                Section(header: Text("Invoice Information")) {
                    TextField("Invoice Number", text:$invoiceNumber)
                    TextField("Description", text:$_description)
                    DatePicker(selection: $dueDate, in: Date()..., displayedComponents: .date) {
                        Text("Due date")
                    }
                    Picker("Currency", selection: $currency) {
                       ForEach(0 ..< Self.currencies.count) {
                           Text(Self.currencies[[=13=]])
                       }
                    }
                }
                Section(header: Text("Items")) {
                    List(lineItems, id: \.self) { item in
                        LineItemRow(lineItem: item)
                        Text(item.productName)
                    }
                    TextField("Product SKU", text: $productSKU)
                    TextField("Poduct Name", text: $productName)
                    TextField("Unit Price", text: $unitPrice, formatter: DoubleFormatter())
                    Picker("Quantity", selection: $quantity) {
                        ForEach(0 ..< 10) {
                            Text("\([=13=])")
                        }
                    }
                    Button(action: {
                        self.addLineItem(lineItem: LineItem(productSku:$productSKU,
                                                            productName:$productName,
                                                            quantity:$unitPrice,
                                                            unitPrice:$quantity))
                        print($lineItems)
                    }, label: {
                        Text("Add line item")
                    })

                }
                Section(header: Text("Totals")) {
                    Text("\(totalAmount)")
                }
                Section {
                    Button(action: {
                        print(Text("Send"))
                    }, label: {
                        Text("Send invoice")
                    })
                }
            }.navigationBarTitle("Invoice Details")
        }
    }
}
List(lineItems, id: \.productSku) { item in <-- I get the error when I add this

您的项目是字典,但字典没有 .productSku 键路径,所以出错。

我认为最简单和正确的方法是将 Item 作为结构

struct LineItem {
   var productSku: String
   ...
}

...

@State private var lineItems = [LineItem]()
@State private var lineItem = LineItem()

...

List(lineItems, id: \.productSku) { item in