File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Sources/SwiftCrossUI/State Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 31
31
/// var nested = NestedState()
32
32
/// }
33
33
/// ```
34
+ ///
35
+ /// To use an observable object as part of a view's state, use the ``State`` property
36
+ /// wrapper. It'll detect that it's been given an observable and will forward any
37
+ /// observations published by the object's ``ObservableObject/didChange`` publisher.
38
+ ///
39
+ /// ```swift
40
+ /// class CounterState: ObservableObject {
41
+ /// @Published var count = 0
42
+ /// }
43
+ ///
44
+ /// struct CounterView: View {
45
+ /// @State var state = CounterState()
46
+ ///
47
+ /// var body: some View {
48
+ /// HStack {
49
+ /// Button("-") {
50
+ /// state.count -= 1
51
+ /// }
52
+ /// Text("Count: \(state.count)")
53
+ /// Button("+") {
54
+ /// state.count += 1
55
+ /// }
56
+ /// }
57
+ /// }
58
+ /// }
59
+ /// ```
34
60
public protocol ObservableObject : AnyObject {
35
61
/// A publisher which publishes changes made to the object. Only publishes changes made to
36
62
/// ``Published`` properties by default.
You can’t perform that action at this time.
0 commit comments