As far as I read about conditional Views this code should work. But it doesn't.
It compiles without warnings or errors. It loads up and displays fine, on simulator and on device. But every time I scroll the List upwards from the end, as soon as this if condition { Text() } should become visible the app crashes with
This is not limited to watchOS. It reproduces the same way in iOS, just the Texts have to be longer so that the if condition { Text() } becomes invisible when scrolling.
I have worked around the error with an array, conditional ranges and two ForEach() blocks.
My question basically is: am I not getting it or did I find a bug in Xcode / SwiftUI? Should my code above work? What could I have done different to make it work with the simple list of Texts?
Background: I also have a TabView with an if condition { MyTab() } which works without crashing. Do I have to worry that this might crash in the same way? Should I work around this as well before shipping?
PS: I am using Xcode 12.4 (12D4e)
Code Block struct Consts { static let myCondition = false /* no difference if true or false */ } struct ContentView: View { @State var toggle: Bool = false var body: some View { List() { Text("Short Text is in first line") Text("Second Line Text is a little longer but not much") if Consts.myCondition { Text("This is a conditional text. As in: when the user hasn't purchased the option he / she don't need a hint how to use this feature.") /* } else { Text("else doesn't help either.") */ } Toggle("I also have a toggle but it has nothing to do with this.", isOn: $toggle) Text("Here we have a longer Text. Dont know what to type any more.") Text("More text which is longer than a few lines.") Text("Not so long Text") } .navigationTitle("Hints & Settings") } }
It compiles without warnings or errors. It loads up and displays fine, on simulator and on device. But every time I scroll the List upwards from the end, as soon as this if condition { Text() } should become visible the app crashes with
Code Block Fatal error: file SwiftUI, line 0 2021-03-07 06:36:26.548126+0100 WTFif WatchKit Extension[23163:641812] Fatal error: file SwiftUI, line 0
This is not limited to watchOS. It reproduces the same way in iOS, just the Texts have to be longer so that the if condition { Text() } becomes invisible when scrolling.
I have worked around the error with an array, conditional ranges and two ForEach() blocks.
Code Block struct ContentView: View { let myHints = ["Short Text is in first line", "Second Line Text is a little longer but not much", "This is a conditional text. As in: when the user hasn't purchased the option he / she don't need to hint how to use this feature.", "Here we have a longer Text. Dont know what to type any more.", "More text which is longer than a few lines.", "Not so long Text"] var myUpperRange: ClosedRange<Int> { if Consts.myCondition { return 0...1 } else { return 0...2 } } var myLowerRange: ClosedRange<Int> { return 3...5 } @State var toggle: Bool = false var body: some View { List() { ForEach (myUpperRange, id: \.self) { i in Text(myHints[i]) } Toggle("I also have a toggle but it has nothing to do with this.", isOn: $toggle) ForEach (myLowerRange, id: \.self) { i in Text(myHints[i]) } } .navigationTitle("Hints & Settings") } }
My question basically is: am I not getting it or did I find a bug in Xcode / SwiftUI? Should my code above work? What could I have done different to make it work with the simple list of Texts?
Background: I also have a TabView with an if condition { MyTab() } which works without crashing. Do I have to worry that this might crash in the same way? Should I work around this as well before shipping?
PS: I am using Xcode 12.4 (12D4e)