I'm trying to generate a row buttons for rock, paper, and scissors (3 buttons) using a ForEach loop, but it's not working.
let choices = ["rock", "paper", "scissors"]
HStack {
ForEach(choices, id: \.self) {
Button {
MoveButton(name: $0.capitalized, image: $0)
}
}
}
At the ForEach line, I'm getting error:
Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored. Insert '_ in'
accompanied by this error at the Button line:
Contextual closure type '() -> MoveButton' expects 0 arguments, but 1 was used in closure body
This, however, works
HStack(spacing: 15) {
ForEach(choices, id: \.self) {
MoveButton(name: $0.capitalized, image: $0)
}
}
Writing ForEach using indexes also works
HStack(spacing: 15) {
ForEach(0..<3) { index in
Button {
print("Buttons were generated!")
} label: {
MoveButton(name: choices[index].capitalized, image: choices[index])
}
}
}
Could someone help me understand why reading directly into the array is causing problems with Button? Thanks!
For context, MoveButton is just a separate view generating button appearance. It's not a button itself (hence why I'm trying to wrap it in a Button).
struct MoveButton: View {
var name: String
var image: String
var body: some View {
VStack {
Image(image)
.resizable()
.frame(width: 50, height: 50)
.frame(width: 100, height: 100)
.background(Color("color-background-alt"))
.cornerRadius(10)
.padding(.bottom, 10)
.shadow(color: .black.opacity(0.15), radius: 5, x: 0, y: 5)
Text(name)
.font(.custom("Inter-Medium", size: 14))
}
}
}