Using APIs introduced in iOS minor releases

I have a Swift package that uses SwiftUI.Font.width that was introduced in iOS 16.1 (and not like the documentation suggests in iOS 16.0)

Using this function in Xcode 14.0 (iOS 16.0) results in a compilation error.

I tried using a conditional compilation statement but I still receive a compilation error in Xcode 14.0.

if #available(iOS 16.1, *) {
  var font = Font.system(.headline)
  font = font.width(Font.Width.condensed)
} 

My Swift package has minimum swift-tools-version of 5.5. and even changing to 5.7 would mean that developers could use Xcode 14 theoretically :frowning:

Any suggestions?

There’s no good solution for this as we can’t check for SDK versions at build time or dynamically check for symbols at runtime. What you can do is a build time check for the Swift version (#if swift(>=5.8)) around the checks for the new API. Technically it’s not a guarantee but Apple’s inflexibility with SDK versions in Xcode works in your favor here, so it should be stable.

2 Likes

Thanks @Jon_Shier for sharing. That is an option :slight_smile:

#if swift(>=5.7.1)
  if #available(iOS 16.0, *) {
      var font = Font.system(.headline)
      font = font.width(Font.Width.condensed)
  }
#endif

but I also realized that I could specify // swift-tools-version: 5.7.1 as the minimum tools version and this is essentially forcing developers to use Xcode 14.1 with iOS SDK 16.1.

Your proposal has the charm that the Swift package generally allows lower swift tools version / Xcode versions but developers would not benefit from the code change (which might or might not be critical)