Reputation: 475
I'm trying to do this:
struct ContentView : View {
struct Element {
var color: Color
}
@State var array = [Element, Element, Element]
ForEach(array){ element in
Rectangle()
.foregroundColor(element.color)
.tapAction {
element.color = Color.red
}
I can't do that because the element is a let constant.
How would I go about making the element a variable so that I can change its properties?
*Edited to add more code.
Upvotes: 0
Views: 186
Reputation: 40509
Ok, you should rewrite your code like this:
import SwiftUI
struct ContentView : View {
struct Element {
var color: Color
}
@State private var array = [Element(color: .red), Element(color: .green), Element(color: .blue)]
var body: some View {
ForEach(0..<array.count) { i in
return Rectangle()
.foregroundColor(self.array[i].color)
.tapAction {
self.array[i].color = Color.red
}
}
}
}
Upvotes: 1