### Configure Storage Types with Macros Source: https://context7.com/kc-2001ms/swiftstorage/llms.txt Demonstrates how to use the @Storage macro and @Attribute property wrapper to define storage locations, including standard UserDefaults and App Group suites. ```swift import SwiftStorage @Storage final class MultiDestinationSettings { var localSetting: Bool @Attribute(type: .localWith(suite: "group.com.example.myapp"), key: "SharedValue") var sharedSetting: Int @Attribute(type: .localWith(suite: nil), key: "CustomKey") var customSetting: String init() { self.localSetting = false self.sharedSetting = 0 self.customSetting = "" } } ``` -------------------------------- ### Direct StorageManager Usage Source: https://context7.com/kc-2001ms/swiftstorage/llms.txt Explains how to interact directly with the StorageManager class for manual read/write operations, including handling Codable objects and custom suites. ```swift import SwiftStorage let manager = StorageManager(type: .local, key: "MyCustomKey") manager.set(value: "Hello, World!") let stringValue: String = manager.get(defaultValue: "default") struct UserProfile: Codable { var id: Int var name: String } let profileManager = StorageManager(type: .local, key: "UserProfile") profileManager.set(value: UserProfile(id: 1, name: "John")) let profile: UserProfile = profileManager.get(defaultValue: UserProfile(id: 0, name: "")) ``` -------------------------------- ### Define a Persistent Model with SwiftStorage Source: https://github.com/kc-2001ms/swiftstorage/blob/main/README.md Demonstrates how to define a data model using the @Storage macro. This automatically handles the persistence of properties to UserDefaults. ```swift import SwiftStorage @Storage final class SwiftStorageModel { var storedValue: Bool init() { self.storedValue = false } } ``` -------------------------------- ### Integrate SwiftStorage Model in SwiftUI Source: https://github.com/kc-2001ms/swiftstorage/blob/main/README.md Shows how to instantiate and use a @Storage-enabled model within a SwiftUI view, allowing for reactive UI updates and automatic data saving. ```swift import SwiftUI struct SwiftStorageView: View { @State private var swiftStorage = SwiftStorageModel() var body: some View { NavigationStack { Form { Toggle("Bool", isOn: $swiftStorage.storedValue) } } } } ``` -------------------------------- ### StorageManager Class Source: https://context7.com/kc-2001ms/swiftstorage/llms.txt Direct interface for reading and writing data to storage. ```APIDOC ## StorageManager ### Description Provides manual control over storage operations. Useful for advanced use cases where property wrappers are not applicable. ### Methods - set(value: T): Persists a value to the defined key. - get(defaultValue: T) -> T: Retrieves a value, returning the provided default if the key is missing. ### Example ```swift let manager = StorageManager(type: .local, key: "MyKey") manager.set(value: "Hello") let val: String = manager.get(defaultValue: "Default") ``` ``` -------------------------------- ### Storable Protocol Source: https://context7.com/kc-2001ms/swiftstorage/llms.txt Explains which data types are supported for automatic persistence. ```APIDOC ## Storable Protocol ### Description Defines types that can be automatically persisted. Includes primitive types, collections, and Codable objects. ### Supported Types - Primitives: String, Int, Double, Float, Bool - Foundation: Data, Date - Collections: Array, Dictionary - Custom: Any type conforming to Codable ### Example ```swift @Storage final class DataStore { var tags: [String] var scores: [String: Int] } ``` ``` -------------------------------- ### Configure Persistence Exclusion Source: https://github.com/kc-2001ms/swiftstorage/blob/main/README.md Illustrates how to prevent specific properties from being saved by using the @Transient macro or by declaring properties as constants. ```swift import SwiftStorage @Storage final class SwiftStorageModel { var storedValue: Bool @Transient var temporaryValue: String let constantValue: String init() { self.storedValue = false self.temporaryValue = "" self.constantValue = "" } } ``` -------------------------------- ### SwiftUI Preferences with SwiftStorage Source: https://context7.com/kc-2001ms/swiftstorage/llms.txt This Swift code defines a data model 'AppPreferences' using SwiftStorage's @Storage macro for automatic persistence. It includes standard properties, a custom key for launch count, App Group sharing for widget data, and transient properties. A SwiftUI View 'PreferencesView' is provided to interact with these preferences, demonstrating how to bind UI elements to the persisted properties and update them. ```swift import SwiftUI import SwiftStorage @Storage final class AppPreferences { // Standard persisted properties var darkMode: Bool var accentColorIndex: Int var userName: String var notificationsEnabled: Bool // Custom key for backward compatibility @Attribute(key: "app_launch_count") var launchCount: Int // Shared with widget via App Group @Attribute(type: .localWith(suite: "group.com.myapp"), key: "WidgetData") var widgetMessage: String // Transient - not persisted @Transient var isOnboarding: Bool @ObservationIgnored var internalState: String init() { self.darkMode = false self.accentColorIndex = 0 self.userName = "" self.notificationsEnabled = true self.launchCount = 0 self.widgetMessage = "Welcome!" self.isOnboarding = true self.internalState = "" } } struct PreferencesView: View { @State private var preferences = AppPreferences() let accentColors = ["Blue", "Purple", "Green", "Orange"] var body: some View { NavigationStack { Form { Section("Appearance") { Toggle("Dark Mode", isOn: $preferences.darkMode) Picker("Accent Color", selection: $preferences.accentColorIndex) { ForEach(0..