Skip to content

Commit

Permalink
Update Default.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored Sep 25, 2024
1 parent 8708fd5 commit 1c9262c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Sources/DefaultsMacros/Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import Defaults
import Foundation

/**
Attached macro that adds support for using ``Defaults`` in ``@Observable`` classes. **Important**: to
prevent issues with ``@Observable``, you'll need to also add ``@ObservationIgnored`` to the attached
property.
Attached macro that adds support for using ``Defaults`` in ``@Observable`` classes.
- Important: To prevent issues with ``@Observable``, you need to also add ``@ObservationIgnored`` to the attached property.
This macro adds accessor blocks to the attached property similar to those added by `@Observable`.
For example, given the following source:
```swift
@Observable
final class CatModel {
@Default(Defaults.Keys.cat)
@Default(.cat)
@ObservationIgnored
private var catName: String
var catName: String
}
```
Expand All @@ -24,14 +25,14 @@ The macro will generate the following expansion:
@Observable
final class CatModel {
@ObservationIgnored
private var catName: String {
var catName: String {
get {
access(keypath: \.catName)
return Defaults[Defaults.Keys.cat]
return Defaults[.cat]
}
set {
withMutation(keyPath: \catName) {
Defaults[Defaults.Keys.cat] = newValue
Defaults[.cat] = newValue
}
}
}
Expand Down

0 comments on commit 1c9262c

Please sign in to comment.