I have some swiftUI code that used to work, but somewhere, in the recent xcode updates it stoped working.
The following simple example demonstrates my problem:
struct ContentView: View {
@State var inputText: String = "0"
var body: some View {
let value = Int(inputText) ?? 0
return VStack {
TextField("Enter Value", text: $inputText)
HStack {
ForEach(0..<8, content: { index in
Text("\(self.nibbleFor(value, pos: index))")
if index == 3 { } // <-- works when this line is commented out.
})
}
}
}
func nibbleFor(_ value: Int, pos: Int) -> UInt8 {
return UInt8((value >> (4 * (7-pos))) & 0x0F)
}
}
If you run this, you get a text field that asks for a number. As you enter the number, the 8 text fields below it shows the nibble (4-bit) values for the value entered.
I use a ForEach on line 10 to generate the 8 nibble values. In my project's code, I also use an "if" statement to add other elements based on the nibble being displayed. My problem is, that as soon as I add any kind of "if", as shown in line 12, then it stops updating the Text in line 11.
If the "if" statement on line 12 is comented out, then it works.
I can add multiple HStacks, and type the code out as a workaround, but that is not very elegant.
Any idea why the if on line 12 breaks the updating of the UI?