Custom Property Wrappers for SwiftUI

A Swift property wrapper adds a layer of separation between code that defines a property and code that managers how a property is stored. By creating custom property wrappers, we write the management code once when we define the wrapper, and then reuse this code when we apply the wrapper to properties. In this post, Dave Delong shares some useful property wrappers that he has come up with while working on SwiftUI.

Swift types with @AppStorage and @SceneStorage

Introduced this year, AppStorage is a SwiftUI property wrapper that reflects a value from UserDefaults which allows for easy storage and observation of app data. Similarly, SceneStorage is a property wrapper that reads and writes to persisted, per-scene storage. They are both super useful, but as they are backed by plists, they are limited in the types of data they support. In this post, Federico Zanetello shows us how we can extend the Published SwiftUI property wrapper to provide similar storage functionality for Codable types.

Dependency Injection via Property Wrappers

Property Wrappers in Swift allow us to add a layer of separation between code that manages how a property is stored and the code that defines a property. It's a powerful feature with a lot of potential use cases. Dependency injection is a software engineering technique used to supply dependencies to an object or value type. In this post, Kilo Loco demonstrates how we can use Swift property wrappers to pass dependencies to a SwiftUI view. It's a cool idea.