https://github.com/3sidedcube/userdefault
A simple property wrapper of the `UserDefaults`
https://github.com/3sidedcube/userdefault
Last synced: 12 months ago
JSON representation
A simple property wrapper of the `UserDefaults`
- Host: GitHub
- URL: https://github.com/3sidedcube/userdefault
- Owner: 3sidedcube
- License: mit
- Created: 2020-11-16T16:52:00.000Z (over 5 years ago)
- Default Branch: develop
- Last Pushed: 2023-07-31T14:30:17.000Z (almost 3 years ago)
- Last Synced: 2025-03-24T20:45:38.914Z (about 1 year ago)
- Language: Swift
- Size: 35.2 KB
- Stars: 0
- Watchers: 8
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UserDefault
Simple property wrapper of the [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults).
## Example
Using the `UserDefault` `@propertyWrapper` allows you to cleanly keep your `UserDefaults` logic all in one place.
What you can persist is not limited to the supported types outlined in the [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults).
`Codable` conformance is provided. For example:
```swift
/// A user model
struct User: Codable: UserDefaultsElement {
// ...
}
/// An animal model
struct Animal: Codable: UserDefaultsElement {
/// Default `Animal` to return from the `UserDefaults`
static var `default`: Animal {
// ...
}
// ...
}
/// Set of entities stored in the `UserDefaults`
struct UserDefaultValues {
/// Example of optional `Codable`
@UserDefault("user", defaultValue: nil)
static var user: User?
/// Example of non-optional `Codable`
@UserDefault("animal", defaultValue: .default)
static var animal: Animal
/// Example of `Int`
@UserDefault("age", defaultValue: 18)
static var age: Int
/// Example of `Bool`
@UserDefault("isLoggedIn", defaultValue: false)
static var isLoggedIn: Bool
}
```
## Acknowledgements
https://www.avanderlee.com/swift/property-wrappers/