Skip to content
Vaida12345Public

About

A type-safe wrapper for `UserDefaults`.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

24 Commits

Folders and files

Repository files navigation

Defaults

A type-safe wrapper for UserDefaults.

Type-safe user defaults

This package utilizes a singleton named standard to represent the default suite of user preferences. By using this singleton, you can access your stored user preferences as instance properties.

let enabled = Defaults.standard.memorySaver

To declare a user default key, you create an extension on Defaults.Keys.

extension Defaults.Keys {

    /// Indicates whether memory saver is enabled.
    var memorySaver: Defaults.Key<Bool> {
        .init("memory_saver", default: false)
    }
}

Please note that the keys are defined as instance properties to support Xcode's autocomplete feature for @dynamicMemberLookup.

Integration with SwiftUI

You can retrieve and observe defaults using AppStorage, similar to how you can access UserDefaults using its identifier.

@AppStorage(\.memorySaver) private var enabled
  • Warning: Optional value must have a default value of nil to be used in AppStorage.

Getting Started

Defaults uses Swift Package Manager as its build tool. If you want to import in your own project, it's as simple as adding a dependencies clause to your Package.swift:

dependencies: [
    .package(url: "https://github.com/Vaida12345/Defaults.git", from: "1.0.0")
]

and then adding the appropriate module to your target dependencies.

Using Xcode Package support

You can add this framework as a dependency to your Xcode project by clicking File -> Swift Packages -> Add Package Dependency. The package is located at:

https://github.com/Vaida12345/Defaults.git

Documentation

This package uses DocC for documentation, view on Github.

About

A type-safe wrapper for `UserDefaults`.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages