错误:“FetchedResults<Item>.Element”类型的值(又名“Item”)没有成员“price2pay”

问题描述 投票:0回答:1

我试图循环访问我的核心数据数据库以获取所有商品的所有价格,但出现错误,“FetchedResults.Element”类型的值(又名“Item”)下面没有成员“price2pay”我的 CartView.swift。有人可以帮我解决这个问题吗?

//
//  CartView.swift
//  PonnasKitchen
//
//  Created by Sricharan Ponna on 02/08/2024.
//  Copyright © 2024 Sricharan Ponna. All rights reserved.
//

import SwiftUI

func forLoop() {
    @FetchRequest(sortDescriptors: []) var checkout: FetchedResults<Item>
    @Environment(\.managedObjectContext) var moc
    var subTotal: Int32 = 0
    do {
        for i in checkout {
            subTotal = try! i.price2pay + subTotal // Error is here
        }
    }
}

struct CartView: View {
    
    @FetchRequest(sortDescriptors: []) var checkout: FetchedResults<Item>
    @Environment(\.managedObjectContext) var moc
    
    @State var subTotal: Int32 = 0
    @State var productPrices: [Int32] = []
    
    
    
    var body: some View {
        
        NavigationStack {
            List {
                ForEach(checkout) { product in
                    VStack(alignment: .leading, spacing: 8) {
                        
                        Text((product.name ?? "Chicken Biryani"))
                            .font(.system(size: 20, design: .rounded).bold())
                        
                        Text("Quantity: \(product.qty)")
                            .font(.callout.bold())
                        
                        Text("Price of Each: £\(Int32(product.price))")
                            .font(.callout.bold())
                        
                        Text("Price to pay: £\(product.price2Pay)")
                            .font(.callout.bold())
                    }
                    .swipeActions {
                        Button(role: .destructive) {
                            do {
                                let item2Delete = try moc.existingObject(with: product.objectID)
                                moc.delete(item2Delete)
                                Task(priority: .background) {
                                    try await moc.perform {
                                        try moc.save()
                                    }
                                }
                            } catch {
                                print(error)
                            }
                        } label: {
                            Label("Delete", systemImage: "trash.fill")
                        }
                        
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                }
            }
            HStack() {
                Text("Subtotal: ")
                    .padding(.horizontal)
                Spacer()
                Text("£\(subTotal).00")
                    .padding(.horizontal)
                Spacer()
                NavigationLink(destination: PurchaseView()) {
                    HStack {
                        Image(systemName: "creditcard.and.123")
                            .symbolVariant(.fill)
                        Divider()
                            .padding(.vertical, 8)
                        Text("Proceed to Buy")
                    }
                    .frame(minWidth: 150, maxHeight: 55)
                    .background(.mint)
                    .foregroundColor(.white)
                    .clipShape(RoundedRectangle(cornerRadius: 8))
                }
            }
        }
        .navigationTitle("Checkout")
    }
}

struct ItemCardView: View {
    
    
    var body: some View {
        HStack {
            Text("card")
                .font(.system(size: 25))
        }
    }
}

//struct CartTitleView: View {
//    
//    var body: some View {
//        VStack(content: {
//            Text("Cart")
//                .font(.title2)
//                .fontWeight(.heavy)
//        })
//    }
//}

#Preview {
    CartView()
}

我试图循环访问我的核心数据数据库以获取所有商品的所有价格,但出现错误,“FetchedResults.Element”类型的值(又名“Item”)下面没有成员“price2pay”我的 CartView.swift。有人可以帮我解决这个问题吗?

ios swift swiftui core-data
1个回答
-3
投票

、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 、 。和你的朋友们在一起娜娜。 https://ithbat.om/

© www.soinside.com 2019 - 2024. All rights reserved.