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

7
  • If the code doesn't build I assume you get a build error(s)? Commented Oct 28, 2019 at 12:25
  • This error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions Commented Oct 28, 2019 at 12:31
  • What happens if you add the id: parameter as well for the inner loop? Another posiible option is to flatten the array of arrays to a single array of names before doing ForEach Commented Oct 28, 2019 at 12:39
  • I can't change the array because for what I have to show I need it like this Commented Oct 28, 2019 at 12:41
  • You won't change the array, just make a new local one to be used for this view. Example from another question on how to flatten an array of arrays Commented Oct 28, 2019 at 12:43

1 Answer 1

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()
                    }
                }
            })
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.