Home > Software engineering >  Get Total of each item inside ForEach
Get Total of each item inside ForEach

Time:09-10

I have two CoreData Entities with a one-to-many-relationship. My first problem was to display the total value of the subentity on the mainentity view, which I could solve with some help. The next problem I'm currently facing, is that I can't display the Total value of the main entity.

With the extension it is now possible to display the total value of each depot. But I also need the total value of all depots, which I'm not able to display.

extension Depot {
    func sumOfDepot() -> Double {
        aktienArray.map(\.a_purchValue).reduce(0,  )
    }
}

struct Portfolio: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Depot.d_name, ascending: true)],
        animation: .default)
    private var depots: FetchedResults<Depot>
   
//DEPOT LIST
                    VStack(alignment: .leading) {
                        ScrollView(showsIndicators: false) {
                            Color.clear
                                .padding(.bottom, g.size.height / -200)
                            ForEach(depots) { depot in

                                ZStack {
                                    NavigationLink(destination: DepotDetail(depot: depot)) {
                                        VStack(alignment: .leading, spacing: 6) {
                                            HStack {
                                                VStack (alignment: .leading){
                                                    HStack (alignment: .bottom) {
                                                        
                                                    Text("Depot Value")
                                                
                                                    Text("\(depot.sumOfDepot() as NSNumber, formatter: formatter) €")  // SHOWS VALUE OF ONE DEPOT
                                                }
                                                Spacer()
                                                
                                            }
                                            Spacer()
                                            Spacer()
                                            Spacer()
                                        }
                                        .padding(.horizontal, g.size.width / 40)
                                        .frame(height: UIScreen.main.bounds.height / 4.7)
                                        .frame(maxWidth: .infinity)
                                        .background(RoundedRectangle(cornerRadius: 15).fill(Color.white))
                                    }.buttonStyle(FlatLinkStyle())
                                    
                                }
                            }

CodePudding user response:

you could try this:

 Text("total: \(depots.map{ $0.sumOfDepot() }.reduce(0,  ))") 

to get the total value of all depots and display it in a Text view.

  • Related