# pfw-sharing > Share and persist values across your application with the Sharing library - Author: LoopingStudio - Repository: LoopingStudio/ApertureTokenManager - Version: 20260206182004 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/LoopingStudio/ApertureTokenManager - Web: https://mule.run/skillshub/@@LoopingStudio/ApertureTokenManager~pfw-sharing:20260206182004 --- --- name: pfw-sharing description: Share and persist values across your application with the Sharing library license: Proprietary. LICENSE has complete terms metadata: short-description: Share and persist values across your application --- # Sharing ## Goal Share and persist values across your application with the [Sharing] library. [Sharing]: http://github.com/pointfreeco/swift-sharing ## Quick start 1. Add the `swift-sharing` package dependency from `2.0.0` or newer. 2. Add the `Sharing` product to your target's dependencies. 3. `import Sharing` as needed ## API interface Full API interface for when solution can't be found elsewhere: `references/interface/Sharing.swiftinterface` ## How to persist a value to app storage (user defaults) Use the `appStorage` strategy: ```swift @Shared(.appStorage("isReduceMotionEnabled")) var isReduceMotionEnabled = false ``` * **DO NOT** use invalid characters (`.`, `@`) in app storage keys ## How to persist a value to the file system Use the `fileStorage` strategy with a codable value: ```swift @Shared(.fileStorage(.applicationSupportDirectory.appending(component: "settings.json"))) var settings = Settings() ``` ## How to add a type-safe default to a strategy ### Step 1: Extend `SharedKey` (or `SharedReaderKey`) with the default ```swift extension SharedKey where Self == AppStorageKey.Default { static var isReduceMotionEnabled: Self { Self[.appStorage("isReduceMotionEnabled"), default: false] } } ``` ### Step 2: Use leading dot-syntax as the strategy ```swift @Shared(.isReduceMotionEnabled) var isReduceMotionEnabled ``` ## How to share a value type as if it were a reference Use `@Shared` without a strategy: ```swift @Shared var currentUser: User ``` ## How to bind a `@Shared` value to a SwiftUI view Pass the projected `$shared` value to `Binding.init(_:)`: ```swift @Shared var isReduceMotionEnabled: Bool Toggle("Reduce motion", isOn: Binding($isReduceMotionEnabled)) ``` ## How to use `@Shared` in an observable model Use `@ObservationIgnored` (`@Shared` manages its own observation): ```swift @ObservationIgnored @Shared(.appStorage("isReduceMotionEnabled")) var isReduceMotionEnabled = false ``` ## How to transform a shared parent to a shared child ```swift @Shared var settings: Settings $settings.isReduceMotionEnabled // Shared ``` ## How to unwrap a shared optional value ```swift @Shared var currentUser: User? if let unwrappedSharedUser = Shared($currentUser) { ... } ``` ## How to observe changes to a shared value Using the Observation framework: ```swift @Shared var currentUser: User? let isLoggedInAsyncSequence = Observations { currentUser != nil } ``` Using the Combine framework: ```swift @Shared var currentUser: User? let isLoggedInPublisher = $currentUser.publisher.map { $0 != nil } ``` * **DO** prefer Observation over Combine ## How to persist a value to a custom backend TODO