I've been trying to use the ternary operator in SwiftUI to make some view modifiers "dynamic" based on the value of a @State var I declared. But, seems like SwiftUI doesn't fully support this operator inside view modifiers. Is it a bug? What am I missing here?
struct Test: View {
@State var selectedIdx: Int?
var colors : [Color] = [.red, .orange, .yellow, .green, .blue]
var body: some View {
VStack {
//some other views that change "selectedIdx" (the @State var)
//...
ForEach(0..<colors.count) { idx in
Rectangle()
.frame(width: 70, height: 100)
//some other view modifiers that will be animated
.animation(Animation.easeInOut.delay(self. selectedIdx == nil ? 0.1*Double(self.colors.count - idx) : 0.1*Double(idx)))
}
}
}
}
The error disappears if I remove the ternary operator. I have tried different combinations for the ternary operator but none of them work:
.animation(Animation.easeInOut.delay(0.1*Double(self.selectedIdx == nil ? self.colors.count - idx : idx)))
.animation(Animation.easeInOut.delay(self.selectedIdx == nil ? 0.1*Double(self.colors.count - idx) : Double(idx)))
And even something simple like the following, doesn't work:
.animation(Animation.easeInOut.delay(self.selectedIdx == nil ? Double(idx) : Double(idx)))
Any ideas about the correct use of the ternary operator inside view modifiers?

selectedIdxmember in provided code, did you meanselected? - Asperi.animation(self.selectedIdx == nil ?Animation.easeInOut.delay(0.1*Double(self.colors.count-idx)) : Animation.easeInOut.delay(0.1*Double(idx)))? - cbjeukendrup