From 4fa2f6153238b3a98acbb315e5a556ae924d9569 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Sat, 14 Jun 2025 20:26:36 +0800 Subject: [PATCH] doc: Document TypedUserDefaults usage and update docs --- .../Documentation.docc/Documentation.md | 9 ++-- .../Documentation.docc/GettingStarted.md | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Sources/TypedAppStorage/Documentation.docc/Documentation.md b/Sources/TypedAppStorage/Documentation.docc/Documentation.md index 7bbd3da..089409b 100644 --- a/Sources/TypedAppStorage/Documentation.docc/Documentation.md +++ b/Sources/TypedAppStorage/Documentation.docc/Documentation.md @@ -1,9 +1,9 @@ # ``TypedAppStorage`` -A type-safe way to save and read complex data structures from `@AppStorage`. +A type-safe way to save and read complex data structures from `@AppStorage` and `UserDefaults`. -- Use actual `@AppStorage` underneath -- Support any `Codable` data +- Use actual `@AppStorage` or `UserDefaults` underneath +- Support any `Codable & Sendable` data - Define the key in the data model ## Topics @@ -11,5 +11,6 @@ A type-safe way to save and read complex data structures from `@AppStorage`. ### Essentials - -- ``TypedAppStorage`` - ``TypedAppStorageValue`` +- ``TypedAppStorage`` +- ``TypedUserDefaults`` diff --git a/Sources/TypedAppStorage/Documentation.docc/GettingStarted.md b/Sources/TypedAppStorage/Documentation.docc/GettingStarted.md index b64a451..abf93d8 100644 --- a/Sources/TypedAppStorage/Documentation.docc/GettingStarted.md +++ b/Sources/TypedAppStorage/Documentation.docc/GettingStarted.md @@ -69,3 +69,46 @@ In some cases it might make sense to specify a different default value than the ``` And you can specify a different store just like `@AppStorage`, if you're using things like App Groups. + +### Use outside SwiftUI Views + +When you're not in a SwiftUI context, you can use ``TypedUserDefaults`` to access the same type-safe storage: + +```swift +let typedDefaults = TypedUserDefaults.standard + +// Reading values +let currentFruit = typedDefaults.object(of: PreferredFruit.self) + +// Writing values +let newFruit = PreferredFruit(.moderate, .pear) +typedDefaults.set(newFruit) +``` + +Just like `@TypedAppStorage`, the key and default value are automatically inferred from the type's ``TypedAppStorageValue`` conformance. + +You can also override the key or default value if needed: + +```swift +// Use a different key +let specialFruit = typedDefaults.object( + of: PreferredFruit.self, + forKey: "specialPreferredFruit" +) + +// Use a different default value +let fruitWithCustomDefault = typedDefaults.object( + of: PreferredFruit.self, + defaultValue: .init(.somewhatStale, .banana) +) +``` + +And you can use a different UserDefaults store, just like with the SwiftUI property wrapper: + +```swift +let groupDefaults = TypedUserDefaults( + userDefaults: UserDefaults(suiteName: "group.com.example.app")! +) +let sharedFruit = groupDefaults.object(of: PreferredFruit.self) +``` +