Search code examples
swiftxcodeios13

SwiftUI: Error while using if #available while trying to build to iOS 13 and iOS 14 in Xcode 12 beta 2


So I'm trying to make it so that my app can deploy on iOS 13 (technically I'm wanting iOS 13.5) as well as iOS 14 so I've written these lines of code to test it:

struct ContentView: View {
    var body: some View {
        if #available(iOS 14.0, *) {
            List {
                Text("Cool!")
                Text("Cool!")
                Text("Cool!")
                Text("Cool!")
            }
            .listStyle(InsetGroupedListStyle())
            
        } else {
            List {
                Text("Cool!")
                Text("Cool!")
                Text("Cool!")
                Text("Cool!")
            }
            .listStyle(GroupedListStyle())
            .environment(\.horizontalSizeClass, .regular)
        }
    }
}

Building to iOS 14 works great, however, whenever I build to iOS 13.5 on a physical phone and the simulator, I get this error in the AppDelegate:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

I've made sure to set my iOS Deployment Target to iOS 13.5 and I'm using Xcode 12 beta 2. I have tried using beta 1 with the same result (though I used the same file, so potentially that's why?). I'm not sure if I'm doing anything wrong or if this is a bug. When using the code for iOS 13.5 alone (without the if #available) it works as expected, but only when I add that checker is when my problems arise. Any help would be appreciated!


Solution

  • Unfortunately i can reproduce this bug in Xcode 12 beta 4, so here a workaround. You can wrap iOS 14 code into AnyView and all will compile successfully. In this particular case you can use the ViewModifier to make code look more swifty.

    struct ContentView: View {
        var body: some View {
            List {
                Text("Cool!")
                Text("Cool!")
            }
            .modifier(GroupedListModifier())
        }
    }
    
    struct GroupedListModifier: ViewModifier {
        func body(content: Content) -> some View {
            Group {
                if #available(iOS 14, *) {
                    AnyView(
                        content
                            .listStyle(InsetGroupedListStyle())
                    )
                } else {
                    content
                        .listStyle(GroupedListStyle())
                        .environment(\.horizontalSizeClass, .regular)
                }
            }
        }
    }