Skip to content

Commit

Permalink
Update readme to include @Default macro
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrpb committed Sep 18, 2024
1 parent 6cc37e0 commit 147892d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Defaults[isUnicorn]

### SwiftUI support

#### `@Default`
#### `@Default` in `View`

You can use the `@Default` property wrapper to get/set a `Defaults` item and also have the view be updated when the value changes. This is similar to `@State`.

Expand All @@ -207,6 +207,22 @@ Note that it's `@Default`, not `@Defaults`.

You cannot use `@Default` in an `ObservableObject`. It's meant to be used in a `View`.

#### `@Default` in `@Observable`

With the `@Default` macro, you can use `Defaults` inside `@Observable` classes that use the [Observation](https://developer.apple.com/documentation/observation) framework. Doing so is as simple as importing `DefaultsMacros` as adding two lines to a property (note that adding `@ObservationIgnored` is needed to prevent clashes with `@Observable`):

```swift
import Defaults
import DefailtsMacros

@Observable
final class UnicornManager {
@Default(.hasUnicorn)
@ObservationIgnored
var hasUnicorn: Bool
}
```

#### `Toggle`

There's also a `SwiftUI.Toggle` wrapper that makes it easier to create a toggle based on a `Defaults` key with a `Bool` value.
Expand Down

0 comments on commit 147892d

Please sign in to comment.