Skip to content

Commit b52448f

Browse files
authored
Add FAQ and quick start (#125)
1 parent e138b30 commit b52448f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ To integrate **AppState** into your Swift project, you’ll need to use the Swif
4040

4141
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.
4242

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+
import AppState
49+
import SwiftUI
50+
51+
private extension Application {
52+
var counter: State<Int> {
53+
state(initial: 0)
54+
}
55+
}
56+
57+
struct ContentView: 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+
4371
## Documentation
4472

4573
Here’s a detailed breakdown of **AppState**'s documentation:
@@ -55,6 +83,7 @@ Here’s a detailed breakdown of **AppState**'s documentation:
5583
- [FileState Usage Guide](documentation/usage-filestate.md): Learn how to persist larger amounts of data securely on disk.
5684
- [Keychain SecureState Usage](documentation/usage-securestate.md): Store sensitive data securely using the Keychain.
5785
- [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**.
5887

5988
## Contributing
6089

documentation/faq.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Frequently Asked Questions
2+
3+
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+
func clearCounter() {
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.
28+

0 commit comments

Comments
 (0)