0

我遇到了这个 ForEach 循环的问题。我试图让它遍历一个数组,但它一直给出一个未知错误,上面写着“无法为表达式生成诊断;请提交错误报告并包含该项目'。当我删除 ForEach 循环时,错误消失了。

这是给出错误的文件

struct ReceiptView: View {

let item: ItemsStruct

var body: some View {
    VStack {
        
        // Header
        HStack {
            VStack(alignment: .leading) {
                Text(item.company)
                    .font(.title2)
                    .fontWeight(.semibold)
                Text(item.date)
                    .font(.caption)
            }
            Spacer()
            Menu {
                Text("Menu Item 1")
                Text("Menu Item 2")
                Text("Menu Item 3")
            } label: {
                Image(systemName: "ellipsis.circle")
                    .scaleEffect(1.2)
            }
        }.padding()
        
        
        // FIXME: This ForEach is causing an error
        ForEach(item.products) { prod in
            Text(prod.product)
        }

    }
}

}

这是 ItemsStruct:

public struct ItemsStruct: Identifiable {
    public let id = UUID()
    let company: String
    let date: String
    let total: String
    let products: [itemsArray]

    struct itemsArray {
        let product: String
        let quantity: Int
        let totalPrice: Double
    }

}

关于导致此错误的原因有什么想法吗?

4

1 回答 1

2

你需要另一个可识别的

public struct ItemsStruct: Identifiable {
    
    public let id: UUID = UUID()
    let company: String
    let date: String
    let total: String
    let products: [ItemsArray]
    
}

struct ItemsArray: Identifiable {
    
    let id: UUID = UUID()          // <<: Here!
    let product: String
    let quantity: Int
    let totalPrice: Double
    
}
于 2021-03-25T14:15:45.063 回答