Member-only story
Swift’s Hidden Gem: Understanding the @retroactive Keyword
Introduction
As iOS developers, we often find ourselves working with system frameworks and third-party libraries. Sometimes, we need these types to conform to certain protocols, especially when working with SwiftUI. This is where Swift’s powerful but lesser-known @retroactive
keyword comes into play. In this article, we'll dive deep into what this keyword does, why it's useful, and how to use it effectively in your iOS applications.
For more of this subscribe to my newsletter . Where I share Swift tips that work in production, plus insights on personal growth and tech innovation. This newsletter is not just for iOS but for all the visionaries who dare to think differently — the ones who believe their code can change the world. 🌍
The Challenge: Extending Types We Don’t Own
Imagine you’re building a SwiftUI app and need to present a UIImage
in a sheet. The sheet modifier in SwiftUI requires its content to conform to the Identifiable
protocol. But here's the catch: UIImage
is a system type that we don't own, and it doesn't conform to Identifiable
out of the box.
Traditionally, this would pose a challenge. We can’t modify the original UIImage
class, and simply adding an extension might not be…