ios – Learn how to group Environmental object (Array of Dictionaries) in SwiftUI

0
25


I’m attempting to show group of sections in Checklist SwiftUI.

Under is my code

ContentView.Swift file

struct ContentView: View {
    @EnvironmentObject var appData: FoodItemData
    @State personal var title: String = ""

    var physique: some View {
        Checklist() {
                VStack {
                    ForEach(returnGroupdDate(dataa: $appData.userData)) { $product in
                        Part(header: Textual content(product.countryName).font(.title).foregroundColor(.purple))
                        {
                        HStack(alignment: .high) {
                            Picture(product.foodImage).resizable()
                                .scaledToFit()
                                .body(width: 100, peak: 100)
                            VStack(alignment: .main, spacing: 2){
                                Textual content(product.foodName).font(.title3)
                                Spacer()
                                Textual content("Rs. (product.foodPrice)").font(.title3).padding(.backside)
                                Textual content("Cal. (product.foodCalories)").font(.title3)
                            }.padding(.high, 5)
                            VStack(alignment: .main, spacing: 5) {
                                Picture(systemName: product.isSelected ? "checkmark.sq." :  "sq.").onTapGesture {
                                    product.isSelected.toggle()
                                }.imageScale(.giant)
                                Spacer()
                                Picture(systemName: product.isFavourited ? "coronary heart.fill" : "coronary heart").onTapGesture {
                                    product.isFavourited.toggle()
                                }.imageScale(.giant)
                            }
                            Spacer()
                        }
                        Divider()
                        }
                }   .padding()
                Spacer()
            }
        }
    }
    
    func returnGroupdDate(dataa: FoodItemData) -> [String : [FoodItemsViewModel]] {
        let studentsByLetter = Dictionary(grouping: appData.userData, by: { $0.foodName })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(FoodItemData())
    }
}

struct CheckBoxView: View {
    @Binding var checked: Bool

    var physique: some View {
        Picture(systemName: checked ? "checkmark.sq..fill" : "sq.")
            .foregroundColor(checked ? Shade(UIColor.systemBlue) : Shade.secondary)
            .onTapGesture {
                self.checked.toggle()
            }
    }
}

func groupByCategory(_ gadgets: [FoodItems]) -> [(String, [FoodItems])] {
    let grouped = Dictionary(grouping: gadgets, by: { $0.foodName })
    return grouped.sorted(by: { $0.key < $1.key })
}


FoodItemData.Swift file

import Basis

struct FoodItems: Hashable {
    var foodName: String
    var countryName: String
    var foodPrice: Double
    var foodCalories: Int
    var foodImage: String
    var isSelected: Bool
    var isFavourited: Bool
}

struct FoodItemsViewModel: Identifiable, Hashable {
    let id = UUID()
    
    var foodItems: FoodItems
    
    var foodName: String {
        return foodItems.foodName.capitalized
    }
    
    var countryName: String {
        return foodItems.countryName.capitalized
    }
    
    var foodPrice: Int {
        return Int(foodItems.foodPrice)
    }
    
    var foodCalories: Int {
        return Int(foodItems.foodCalories)
    }
    
    var foodImage: String {
        return foodItems.foodImage
    }

    var isSelected: Bool {
        get {
            return foodItems.isSelected
        }
        set {
            foodItems.isSelected = newValue
        }
    }
    
    var isFavourited: Bool {
        get {
            return foodItems.isFavourited
        }
        set {
            foodItems.isFavourited = newValue
        }
    }
}

class FoodItemData: ObservableObject {
    
    @Printed var userData : [FoodItemsViewModel]
    
    init() {
        userData = [
            FoodItemsViewModel(foodItems: FoodItems(foodName: "India Food 1", countryName: "Indian", foodPrice: 500, foodCalories: 100, foodImage: "IndiaFood1", isSelected: false, isFavourited: false)),
            
            FoodItemsViewModel(foodItems: FoodItems(foodName: "India Food 2", countryName: "Indian", foodPrice: 600, foodCalories: 90, foodImage: "IndiaFood2", isSelected: false, isFavourited: false)),
            
            FoodItemsViewModel(foodItems: FoodItems(foodName: "USA Food 1", countryName: "Chineese", foodPrice: 1000, foodCalories: 80, foodImage: "USAFood1", isSelected: false, isFavourited: false)),
            
            FoodItemsViewModel(foodItems: FoodItems(foodName: "USA Food 2", countryName: "Chineese", foodPrice: 900, foodCalories: 68, foodImage: "USAFood2", isSelected: false, isFavourited: false)),
            
            FoodItemsViewModel(foodItems: FoodItems(foodName: "Russian Food 1", countryName: "Russian", foodPrice: 1200, foodCalories: 79, foodImage: "RussianFood1", isSelected: false, isFavourited: false)),

            FoodItemsViewModel(foodItems: FoodItems(foodName: "Russian Food 2", countryName: "Russian", foodPrice: 1100, foodCalories: 200, foodImage: "RussianFood2", isSelected: false, isFavourited: false)),

            FoodItemsViewModel(foodItems: FoodItems(foodName: "German Food 1", countryName: "German", foodPrice: 1100, foodCalories: 190, foodImage: "GermanFood1", isSelected: false, isFavourited: false)),

            FoodItemsViewModel(foodItems: FoodItems(foodName: "German Food 2", countryName: "German", foodPrice: 1100, foodCalories: 90, foodImage: "GermanFood2", isSelected: false, isFavourited: false)),

        ]
    }
}

Its getting beneath error

Can not convert worth of sort ‘[String : [FoodItemsViewModel]]’ to anticipated argument sort ‘Binding’

Can not convert worth of sort ‘Binding<[FoodItemsViewModel]>’ to anticipated argument sort ‘FoodItemData’
Any options?

I wish to present like this pattern group record
enter image description here