Tour of Sharing: App Storage, Part 2

Episode #306 • Dec 9, 2024 • Subscriber-Only

We show how the @Shared property wrapper, unlike @AppStorage, can be used anywhere, not just SwiftUI views. And we show how @Shared has some extra bells and whistles that make it easier to write maintainable Xcode previews and avoid potential bugs around “string-ly” typed keys and default values.

Previous episode
Tour of Sharing: App Storage, Part 2
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

Brandon

We have now see the basics of using the @Shared property wrapper in a SwiftUI app, and in particular the appStorage persistence strategy that comes with the library. It gives you a tool that is similar to the @AppStorage property wrapper from SwiftUI, but it works outside of views, including observable models. And we even saw that it improves upon some key aspects, such as being animatable.

Stephen

And so this is all looking great, but it gets better. The @Shared property wrapper can be used in many more places than just observable models. It can be used in a SwiftUI view just like vanilla SwiftUI’s @AppStorage, but it can be used in UIKit view controllers. And it behaves the same in all these different contexts, including automatically re-computing the view when the state changes, as well as listening for changes to user defaults directly so that it can update its state.

Let’s explore this, and more…

Using @Shared everywhere


References

Downloads

Sample code

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing