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.
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…