I have an fixed array of allowed colors as “availableColors”
I have a “thisColor” color variable used to fill a rectangle (predefined as “Color(.blue)” in the code below)
I also have a string variable “colorString” (“pink” in the code below)
If “colorString” matches the Color.description of any item in “availableColors”, variable “thisColor” should be assigned that item.
I must be missing something because while I can loop through the colors, I can’t assign the variable color.
The code and preview are below. If the commented line is uncommented, the preview fails with the generic message: Failed to produce diagnostic for expression; please submit a bug report
I am sure I am missing something obvious, since this doesn’t seem to be too weird. But I can’t figure out what.
import SwiftUI
struct testView: View {
let availableColors: [Color] = [.blue,.pink,.gray]
@State private var thisColor = Color(.blue)
var colorString = ".pink"
var body: some View {
VStack {
Text(thisColor.description)
.bold()
Divider()
ForEach(availableColors, id: .self) { colorItem in
Text(colorItem.description)
if(colorItem.description == colorString) {
// thisColor = colorItem
}
}
Rectangle()
.fill(thisColor)
.frame(width:50,height:50)
}
}
}
#Preview {
testView()
}
(I’m not sure how to best summarize the question. I’ll edit with suggestions to best do so)