1

I've this situation in the new SwiftUI, I've a problem to create duoble foreach

struct LineupMod {
    var id: Int = 0
    var mod: Int = 0
    var name: [String] = []
}

This In my view

@ObservedObject var lineupMod = LineupViewModel()
-
-
-
                           ForEach(self.lineupMod.lineupMod, id: \.self) { module in
                                HStack(alignment: .center, spacing: 20, content: {
                                    ForEach(module.name) { name in
                                        Group {
                                            Spacer()
                                            VStack {
                                                Image("Wanda_Nara")
                                                    .resizable()
                                                    .clipShape(Circle())
                                                    .shadow(radius: 10)
                                                    .overlay(Circle().stroke(Color.red, lineWidth: 1))
                                                    .frame(width: 50, height: 50)
                                                Text(name)
                                                    .foregroundColor(Color.white)
                                            }
                                            Spacer()
                                        }
                                    }
                                })
                            }               

Why the code not build? In LineupViewModel i get data from server thanks

Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
Stefano Toppi
  • 626
  • 10
  • 28

1 Answers1

1

try do this. It's always interesting that the id is of type UUID.

struct LineupMod: Hashable {
    var id: UUID = UUID()
    var mod: Int = 0
    var name: [String] = []

    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}
ForEach(lineupMod.lineupMod, id: \.self) { module in
            HStack(alignment: .center, spacing: 20, content: {
                ForEach(module.name, id: \.self) { name in
                    Group {
                        Spacer()
                        VStack {
                            Image("Wanda_Nara")
                                .resizable()
                                .clipShape(Circle())
                                .shadow(radius: 10)
                                .overlay(Circle().stroke(Color.red, lineWidth: 1))
                                .frame(width: 50, height: 50)
                            Text(name)
                                .foregroundColor(Color.white)
                        }
                        Spacer()
                    }
                }
            })
        }
Paloma Bispo
  • 312
  • 3
  • 9