https://github.com/zijievv/user-defaults-observation
A Swift macro adding accessors to properties for reading/writing values in UserDefaults within Observable classes
https://github.com/zijievv/user-defaults-observation
macro macros observable observation swift swift-macro swift-macros swift-package-manager swiftui userdefaults
Last synced: over 1 year ago
JSON representation
A Swift macro adding accessors to properties for reading/writing values in UserDefaults within Observable classes
- Host: GitHub
- URL: https://github.com/zijievv/user-defaults-observation
- Owner: zijievv
- License: mit
- Created: 2023-09-07T16:44:45.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-30T07:48:50.000Z (almost 2 years ago)
- Last Synced: 2024-07-01T16:22:03.781Z (almost 2 years ago)
- Topics: macro, macros, observable, observation, swift, swift-macro, swift-macros, swift-package-manager, swiftui, userdefaults
- Language: Swift
- Homepage:
- Size: 32.2 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UserDefaultsObservation
`ObservableUserDefaults` is a Swift macro adding accessors to properties for reading/writing values in `UserDefaults` within `Observable` classes.
## Usage
Source code:
```swift
import Foundation
import Observation
import UserDefaultsObservation
@Observable
class Model {
@ObservableUserDefaults(key: "text", store: Self.store)
@ObservationIgnored
var text: String = "Text"
@ObservableUserDefaults(key: "value")
@ObservationIgnored
var value: Int = 1
static let store = UserDefaults(suiteName: "Store")!
}
```
Expanded source:
```swift
import Foundation
import Observation
import UserDefaultsObservation
@Observable
class Model {
@ObservationIgnored
var text: String {
@storageRestrictions(initializes: _text)
init(initialValue) {
_text = initialValue
}
get {
access(keyPath: \.text)
let store: UserDefaults = Self.store
return (store.value(forKey: "text") as? String) ?? _text
}
set {
withMutation(keyPath: \.text) {
let store: UserDefaults = Self.store
store.set(newValue, forKey: "text")
}
}
}
@ObservationIgnored private let _text: String
@ObservationIgnored
var value: Int = 1 {
@storageRestrictions(initializes: _value)
init(initialValue) {
_value = initialValue
}
get {
access(keyPath: \.value)
let store: UserDefaults = .standard
return (store.value(forKey: "value") as? Int) ?? _value
}
set {
withMutation(keyPath: \.value) {
let store: UserDefaults = .standard
store.set(newValue, forKey: "value")
}
}
}
@ObservationIgnored private let _value: Int
static let store = UserDefaults(suiteName: "Store")!
}
```
⚠️ Note that when you use the `@ObservableUserDefaults(key:store:)` macro, you need to add the `@ObservationIgnored` macro to the property. Otherwise, `@Observable` will generate accessors that conflict with `ObservableUserDefaults`.
## Installation
### [Swift Package Manager](https://www.swift.org/package-manager/) (SPM)
Add the following line to the dependencies in `Package.swift`, to use the `ObservableUserDefaults` macro in a SPM project:
```swift
.package(url: "https://github.com/zijievv/user-defaults-observation", from: "0.1.0"),
```
In your target:
```swift
.target(name: "", dependencies: [
.product(name: "UserDefaultsObservation", package: "user-defaults-observation"),
// ...
]),
```
Add `import UserDefaultsObservation` into your source code to use the `ObservableUserDefaults` macro.
### Xcode
Go to `File > Add Package Dependencies...` and paste the repo's URL:
```
https://github.com/zijievv/user-defaults-observation
```