微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这个 ForEach 循环不断导致错误?

如何解决为什么这个 ForEach 循环不断导致错误?

我遇到了这个 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
    }

}

对导致此错误的原因有任何想法吗?

解决方法

您需要另一个可识别的

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
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。