You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,34 @@ To integrate **AppState** into your Swift project, you’ll need to use the Swif
40
40
41
41
After installation, refer to the [Usage Overview](documentation/usage-overview.md) for a quick introduction on how to manage state and inject dependencies into your project.
42
42
43
+
## Quick Example
44
+
45
+
Below is a minimal example showing how to define a piece of state and access it from a SwiftUI view:
46
+
47
+
```swift
48
+
importAppState
49
+
importSwiftUI
50
+
51
+
privateextensionApplication {
52
+
var counter: State<Int> {
53
+
state(initial: 0)
54
+
}
55
+
}
56
+
57
+
structContentView: View {
58
+
@AppState(\.counter) var counter: Int
59
+
60
+
var body: some View {
61
+
VStack {
62
+
Text("Count: \(counter)")
63
+
Button("Increment") { counter +=1 }
64
+
}
65
+
}
66
+
}
67
+
```
68
+
69
+
This snippet demonstrates defining a state value in an `Application` extension and using the `@AppState` property wrapper to bind it inside a view.
70
+
43
71
## Documentation
44
72
45
73
Here’s a detailed breakdown of **AppState**'s documentation:
@@ -55,6 +83,7 @@ Here’s a detailed breakdown of **AppState**'s documentation:
55
83
-[FileState Usage Guide](documentation/usage-filestate.md): Learn how to persist larger amounts of data securely on disk.
56
84
-[Keychain SecureState Usage](documentation/usage-securestate.md): Store sensitive data securely using the Keychain.
57
85
-[iCloud Syncing with SyncState](documentation/usage-syncstate.md): Keep state synchronized across devices using iCloud.
86
+
-[FAQ](documentation/faq.md): Answers to common questions when using **AppState**.
This short FAQ addresses common questions developers may have when using **AppState**.
4
+
5
+
## How do I reset a state value?
6
+
7
+
Each `State` provides a `reset()` function that restores the value to the `initial` one defined in your `Application` extension. Call `reset()` when you need to clear user data or return to a default configuration.
8
+
9
+
```swift
10
+
@AppState(\.counter) var counter: Int
11
+
12
+
funcclearCounter() {
13
+
counter.reset() // Resets to the initial value
14
+
}
15
+
```
16
+
17
+
## Can I use AppState with asynchronous tasks?
18
+
19
+
Yes. `State` and dependency values are thread-safe and work seamlessly with Swift Concurrency. You can access and modify them inside `async` functions without additional locking.
20
+
21
+
## Where should I define states and dependencies?
22
+
23
+
Keep all your states and dependencies in `Application` extensions. This ensures a single source of truth and makes it easier to discover all available values.
24
+
25
+
## Is AppState compatible with Combine?
26
+
27
+
You can use AppState alongside Combine by bridging `State` changes to publishers. Observe a `State` value and send updates through a `PassthroughSubject` or other Combine publisher if needed.
0 commit comments