NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Function back deployment

Available from Swift 5.8

Paul Hudson      @twostraws

SE-0376 adds a new @backDeployed attribute that makes it possible to allow new APIs to be used on older versions of frameworks. It works by writing the code for a function into your app’s binary then performing a runtime check: if your user is on a suitably new version of the operating system then the system’s own version of the function will be used, otherwise the version copied into your app’s binary will be used instead.

On the surface this sounds like a fantastic way for Apple to make some new features retroactively available in earlier operating systems, but I don’t think it’s some kind of silver bullet – @backDeployed applies only to functions, methods, subscripts, and computed properties, so while it might work great for smaller API changes such as the fontDesign() modifier introduced in iOS 16.1, it wouldn’t work for any code that required new types to be used, such as the new scrollBounceBehavior() modifier that relies on a new ScrollBounceBehavior struct.

As an example, iOS 16.4 introduced a monospaced(_ isActive:) variant for Text. If this were using @backDeployed, the SwiftUI team might ensure the modifier is available to whatever earliest version of SwiftUI supports the implementation code they actually need, like this:

import SwiftUI

extension Text {
    @backDeployed(before: iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4)
    @available(iOS 14.0, macOS 11, tvOS 14.0, watchOS 7.0, *)
    public func monospaced(_ isActive: Bool) -> Text {
        fatalError("Implementation here")

At runtime, Swift will use the system’s copy of SwiftUI if it has that modifier already, otherwise using the back-deployed version back to iOS 14.0 and similar.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Other changes in Swift 5.8…

Download all Swift 5.8 changes as a playground Link to Swift 5.8 changes

Browse changes in all Swift versions

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.