How to avoid compilation error in Xcode 11.5 when adding custom views and modifiers to the Xcode Library

I face compilation error in Xcode 11.5 for iOS 13.5 even when declaring that my LibraryContentProvider shall be available for iOS 14 only.

Error

Use of undeclared type 'LibraryContentProvider'

Code

Code Block swift
@available(iOS 14.0, *)
struct LibraryContent : LibraryContentProvider {
 @LibraryContentBuilder
 var views: [LibraryItem] {
  LibraryItem(
   Text("Hello WWDC!"),
   title: "Custom View In The Library"
  )
 }
}


How to avoid this? I am asking because I'd like to ship my custom LibraryContentProvider as early as possible for people using Xcode 12 beta but I need to avoid compilation error for Xcode 11.5 users, of course :)

Accepted Reply

Unfortunately LibraryContentProvider and friends requires iOS 14 SDK, so it won't compile with an older one.

There's no good way to detect the SDK in a way that will allow you to exclude type declarations entirely. The best you can do is approximate what SDK is being used based on what Xcode is being used, which in turn can be approximated by which Swift version is being used. Since Xcode 12. ships with Swift 5.3 you can check for that. Something like:

Code Block
#if swift(>=5.3)
@available(iOS 14.0, *)
struct LibraryContent : LibraryContentProvider { 
// library content here
}
#endif


This isn't bulletproof since you're not explicitly checking for the SDK version.

Replies

Unfortunately LibraryContentProvider and friends requires iOS 14 SDK, so it won't compile with an older one.

There's no good way to detect the SDK in a way that will allow you to exclude type declarations entirely. The best you can do is approximate what SDK is being used based on what Xcode is being used, which in turn can be approximated by which Swift version is being used. Since Xcode 12. ships with Swift 5.3 you can check for that. Something like:

Code Block
#if swift(>=5.3)
@available(iOS 14.0, *)
struct LibraryContent : LibraryContentProvider { 
// library content here
}
#endif


This isn't bulletproof since you're not explicitly checking for the SDK version.