New Xcode modifier conditional compilation

i'm using this code with the new searchable modifier available with Xcode-13 beta. is there any way to have some conditional compilation check to compile this with Xcode 12? obviously i can't built with it to those newer platforms but i'd expect it'd be able to compile and deploy to the older platforms.

struct MyView: View {
    @State private var searchText = ""
    
    var body: some View {
        if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) {
            SomeView(searchText: searchText)
                .searchable(text: $searchText) // no member 'searchable' error
        } else {
            // fallback
        }
    }
}

You can use #if compiler(>= 5.5) perhaps, as Swift 5.5 ships with Xcode 13.

struct MyView: View {
    @State private var searchText = ""
    
    var body: some View {
        #if compiler(>= 5.5)
          if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) {
            SomeView(searchText: searchText)
                .searchable(text: $searchText) // no member 'searchable' error
          } else {
            // fallback
          }
        #else
          // fallback
        #endif
    }
}
3 Likes

this works indeed, thanks.

when i run this app under Xcode 13 beta Xcode + iOS14 simulator the app crashes, probably because it is beta after all.

struct ContentView: View {
    @State private var searchText = ""
    
    var body: some View {
        if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) {
            #if compiler(>=5.5)
            Text("Hello-1")
                .searchable(text: $searchText)
            #else
            Text("Hello-2")
            #endif
        } else {
            Text("Hello-3")
        }
    }
}

Xcode 12.5 + iOS 14 simulator: "Hello-3"

Xcode 12.5 + iOS 15 simulator: "Hello-2"

Xcode 13 Beta + iOS 15 simulator: "Hello-1"

Xcode 13 Beta + iOS 14 simulator: crash on load:

" dyld: Symbol not found: _$s7SwiftUI4ViewPAAE10searchable4text9placementQrAA7BindingVySSG_AA20SearchFieldPlacementVtFQOMQ"

Terms of Service

Privacy Policy

Cookie Policy