I'm trying to create a simple code: I have an "array" with three objects, each containing a boolean value. I would like to create a list that creates a button for each object and when I click on the button, the value of the object switches. I tried but I get an error message: "Cannot assign to property: 'n' is a 'let' constant". Do you have any advice or a simple code for me?
import SwiftUI
struct Object {
var id = UUID()
var selected : Bool
}
struct ContentView: View {
var array = [Object( selected: false), Object(selected: false), Object(selected: false)]
var body: some View {
ForEach(array, id: (\.id)) { n in
VStack {
Button( action: { n.selected = true }) { Text("Change")}
}
}
}
}