I have the following simple ContentView:
struct ContentView: View {
@State private var matrix = [BoxModel(id: 0), BoxModel(id: 1), BoxModel(id: 2)]
@State private var chosen = BoxModel(id: -1)
var body: some View {
NavigationView {
GeometryReader { geometry in
ScrollView {
VStack {
let columns = [GridItem(.fixed(50), spacing: 0),
GridItem(.fixed(50), spacing: 0),
GridItem(.fixed(50), spacing: 0)]
LazyVGrid(columns: columns, spacing: 0 ) {
ForEach(matrix) { box in
Button {
chosen = box
} label: {
Text("\(box.number)")
}
}
}
.padding()
Button {
chosen.number = 5
} label: {
Text("Click Me")
}
}
}
}
}
}
}
where the BoxModel is an object like this:
struct BoxModel: Codable, Identifiable, Hashable {
var id: Int
var number: Int
}
When the button is pressed I want the "box" to be updated with the new number. But I want to do this without using the index. I know that if I get the index of the box, I can do something like matrix[0].number = 5 and it'll work.
Is this possible?