Back deploy Swift Function

⋅ 2 min read ⋅ Swift Swift 5.8

Table of Contents

Swift 5.8 introduced a new attribute, @backDeployed, that allows Framework or Libray author to back deploy a self-contained function to older versions of OS.

Before Swift 5.8

Swift libraries, such as the ones present in the SDKs for Apple's platforms, are distributed as dynamic libraries.

Authors of these libraries use @available annotations to indicate the operating system version that a declaration was introduced.

Here is an example of badge modifier, which was introduced in iOS 15.

@available(iOS 15.0, macOS 12.0, *)
extension View {
public func badge(_ count: Int) -> some View
}

If you want to adopt badge(), you must conditionally check for availability before use. And you need to provide a fallback implementation for iOS older than 15.

TabView {
if #available(iOS 15.0, *) {
Text("Hello, World!")
.tabItem {
Label("Home", systemImage: "house")
}
.badge(5)
} else {
Text("Hello, World!")
.tabItem {
Label("Home", systemImage: "house")
}
// 1
.overlay(Text("Fake Badge"))
}
}

1 We need to provide a fallback implementation here.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

What is @backDeployed

@backDeployed is a new attribute that allows Framework or Library author to provide a fallback implementation for older OS versions and allow API consumers to adopt it without any conditionally checking.

As an example, if Apple decided to back deploy the badge modifier to iOS before 15, they can declare a back deploy implementation of the badge function like this.

extension View {
@available(iOS 13.0, *)
@backDeployed(before: iOS 15.0)
public func badge(_ count: Int) -> some View {
self.overlay(Text("Fake Badge"))
}
}

If this back-deploy version is implemented, we don't have to do any checking at the call site.

At runtime, the Swift system will choose the one from the original library if possible and choose back deployed version otherwise.

TabView {
Text("Hello, World!")
.tabItem {
Label("Home", systemImage: "house")
}
.badge(5)
}

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Limitations

The @backDeployed attribute may apply to only:

  • Functions
  • Methods
  • Subscripts
  • Properties may also have the attribute as long as they do not have storage.

There are a lot more details that I left from this article. If you are interested, I encourage you to check out the proposal of Function Back Deployment.


Read more article about Swift, Swift 5.8, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Find Callers of methods or variables in Xcode

Callers or Call Hierarchy shows you all the places where a method, function, or variable is used. You can easily find callers in Xcode.

Next
Allow implicit self for weak self captures, after self is unwrapped

Swift allows implicit self in many places. It removes visual noise and allows developers to focus on things that matter. In Swift 5.8, Swift expands the case, which enables the implicit self to be used.

← Home