When using if #available(iOS 14.0, OSX 11.0, *) inside a ViewBuilder, the application crashes when running on the fallback side (e.g., iOS 13.5).
Bug Report: FB7838831
Workaround: Yes, see below.
Last Version Checked: Fixed on Xcode 12, Beta 5
Dev Forum Discussion: https://developer.apple.com/forums/thread/650818
Sample Code
struct TestView: View {
var body: some View {
Group {
if #available(iOS 14.0, OSX 11.0, *) {
LazyVStack { Color.red }
} else {
VStack { Color.green }
}
}
}
}
The code above causes a crash when running on iOS13.5, but not iOS14.0. If LazyVStack is replaced for something that is available on iOS13.5, the app won’t crash. That is strange, as that part of the code is never executed when running on iOS13.5.
It seems this problem only occurs if the “if #available” is performed inside a ViewBuilder. Mainly, all containers (Group, [HVZ]Stacks, List, ScrollView, etc).
Workaround
By using AnyView to wrap the offending view, the problem goes away.
struct TestView: View {
var body: some View {
ScrollView {
if #available(iOS 14.0, OSX 11.0, *) {
AnyView(LazyVStack { Color.red })
} else {
VStack { Color.green }
}
}
}
}