https://github.com/batuhansk/UserDefault
The simplest way of using the UserDefaults with @propertyWrapper.
https://github.com/batuhansk/UserDefault
persistence propertywrapper swift userdefaults
Last synced: about 1 year ago
JSON representation
The simplest way of using the UserDefaults with @propertyWrapper.
- Host: GitHub
- URL: https://github.com/batuhansk/UserDefault
- Owner: batuhansk
- Created: 2019-09-30T13:29:05.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T16:40:00.000Z (over 4 years ago)
- Last Synced: 2024-08-25T02:01:12.992Z (almost 2 years ago)
- Topics: persistence, propertywrapper, swift, userdefaults
- Language: Swift
- Homepage:
- Size: 12.7 KB
- Stars: 18
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UserDefault
### UserDefault wrapper enables you to use `UserDefaults` in the simplest way with the power of `@propertyWrapper`.
Primitive types of Swift which conforms `Codable` protocol internally are supported. *(String, Int, Float, Double, Data, Bool, Date, URL)*
## Installation
### Swift Package Manager:
To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift:
```swift
dependencies: [
.package(url: "https://github.com/strawb3rryx7/UserDefault.git", from: "master")
]
```
### Manually
Just drag the `UserDefault.swift` file into your project directory. It's all done.
## Usage
```swift
import UserDefault
struct Defaults {
@UserDefault("is_discount_provided", defaultValue: false)
static var isDiscountProvided: Bool
}
```
## How to Modify?
Well, that's pretty simple. You only have to access the variable through the `Defaults` struct, and set it the value.
```swift
Defaults.isDiscountProvided = true
```
In addition, Custom models which conforms `Codable` protocol can be stored too.
### Custom models
```swift
struct User: Codable {
let firstName: String
let lastName: String
}
struct Defaults {
@UserDefault("default_user")
static var defaultUser: User?
}
```
You can specify any `UserDefaults` suite for each one. If you want to store on standard suite, no needed to specify it. Default is `UserDefaults.standard`.
### Usage of custom UserDefaults suite
```swift
struct Contact: Codable {
let firstName: String
let lastName: String
let phoneNumber: String
}
private let appSuite = UserDefaults(suiteName: "com.strawb3rryx7.userdefault.appsuite")!
private let customSuite = UserDefaults(suiteName: "com.strawb3rryx7.userdefault.customsuite")!
struct Defaults {
@UserDefault("primary_contact", suite: appSuite)
static var primaryContact: Contact?
@UserDefault("has_seen_purchase_screen", defaultValue: false, suite: customSuite)
static var hasSeenPurchaseScreen: Bool
}
```