Monday, April 17, 2023

Swift’s Versioned canImport()

Marco Eidinger:

SwiftUI.Font.width(_:) was actually introduced in iOS 16.1 and not in iOS 16.0 as the documentation suggests.


While researching a solution, I stumbled on an interesting fact that #if canImport allows specifying a version that gets checked during compile-time. The version gets compared against the -user-module-version flag in the .swiftmodule file using.


Suppose the framework and the APIs in question are cross-platform. In that case, you can (theoretically) write a single query to determine the build time availability of the API across multiple platforms because module versions tend to be aligned across the aligned platform-specific SDKs.

It allows you to gracefully handle things like APIs introduced midway through the betas even though the overall system/SDK version number hasn’t changed.

But you have to figure out the user-module-version of the framework.

Comments RSS · Twitter · Mastodon

Leave a Comment