SwiftUI 列表 "Generic parameter 'S' could not be inferred"

SwiftUI List "Generic parameter 'S' could not be inferred"

我正在尝试显示 SwiftUI 列表并收到错误,"Generic parameter 'S' could not be inferred"。我曾尝试将其更改为 ForEach,但在 ForEach 处出现相同的错误。错误的位置在下面有注释。

import SwiftUI
import Combine

@available(iOS 13.3, *)
struct MyCustomerListView: View {
    @ObservedObject var custObservable: CustomersObservable = CustomersObservable()

    init()
    {
        UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: Double.leastNonzeroMagnitude))
    }

    var body: some View {
        List(custObservable.customers) { cust in  //  <-- Error is here on List
            NavigationLink(destination: MultipleSignatureView(customer: cust)) {
                Text(cust.companyName)
            }
        }.listStyle(GroupedListStyle())
    }
}

@available(iOS 13.3, *)
class CustomersObservable: ObservableObject {
    @Published var customers: [Cust] = [Cust]()

    init() {
        customers.append(contentsOf: [
            Cust(id: "u", companyName: "Uinta"),
            Cust(id: "v", companyName: "victor"),
            Cust(id: "w", companyName: "wasden")
        ])
    }
}

@available(iOS 13.3, *)
class Cust: Identifiable {
    var id: String
    var companyName: String

    init(id: String, companyName: String) {
        self.id = id
        self.companyName = companyName
    }
}

会是什么?提前致谢。

我终于明白了。该错误根本不直观。我的问题出在这段代码中:

NavigationLink(destination: MultipleSignatureView(customer: cust)) { // <-- wrong type passed
    Text(cust.companyName)
}

我的 MultipleSignatureView class 使用了 String 而不是 Cust 对象。一旦我对我的 MultipleSignatureView 构造函数进行了更改,允许它接收 Cust 对象,它就起作用了。

感谢那些发表评论的人。事后看来,由于我发布的代码是在其他系统上编译的,所以我的问题应该包括我的相关对象。如果有人有类似的问题,我会在这里提供我的前后代码:

之前:

import SwiftUI

@available(iOS 13.3, *)
struct MultipleSignatureView: View {
    var customer: String     // <-- It was choking here. Cust object was being passed
    var body: some View {
        Text(customer)
    }
}

之后:

import SwiftUI

@available(iOS 13.3, *)
struct MultipleSignatureView: View {
    var customer: Cust               // <-- Now it takes a Cust object
    var body: some View {
        Text(customer.companyName)
    }
}