55[ ![ ] ( https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fpointfreeco%2Fswift-perception%2Fbadge%3Ftype%3Dswift-versions )] ( https://swiftpackageindex.com/pointfreeco/swift-perception )
66[ ![ ] ( https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fpointfreeco%2Fswift-perception%2Fbadge%3Ftype%3Dplatforms )] ( https://swiftpackageindex.com/pointfreeco/swift-perception )
77
8- Observation tools for platforms that do not officially support observation .
8+ Swift's Observation tools, back-ported to more platforms .
99
1010## Learn More
1111
@@ -18,11 +18,11 @@ host the [Point-Free][pointfreeco] video series which explores advanced Swift la
1818
1919## Overview
2020
21- The Perception library provides tools that mimic ` @Observable ` and ` withObservationTracking ` in
22- Swift 5.9, but they are backported to work all the way back to iOS 13, macOS 10.15, tvOS 13 and
23- watchOS 6. This means you can start taking advantage of Swift 5.9's observation tools today,
24- even if you can't drop support for older Apple platforms. Using this library's tools works almost
25- exactly as using the official tools, but with one small exception.
21+ The Perception library back-ports ` @Observable ` , ` withObservationTracking ` , and ` Observations ` all
22+ the way back to iOS 13, macOS 10.15, tvOS 13 and watchOS 6. This means you can take advantage of
23+ all of Swift's observation tools today, even if you can't drop support for older Apple platforms.
24+ Using this library's tools works almost exactly as using the official tools, with one small
25+ exception.
2626
2727To begin, mark a class as being observable by using the ` @Perceptible ` macro instead of the
2828` @Observable ` macro:
@@ -54,26 +54,33 @@ struct FeatureView: View {
5454 var body: some View {
5555 WithPerceptionTracking {
5656 Form {
57- Text (model.count . description )
57+ Text (" \( model.count ) " )
5858 Button (" Increment" ) { model.count += 1 }
5959 }
6060 }
6161 }
6262}
6363```
6464
65- It's unfortunate to have to wrap your view's content in ` WithPerceptionTracking ` , however if you
66- forget then you will helpfully get a runtime warning letting you know that observation is not
67- set up correctly:
65+ It's unfortunate to have to wrap your view's content in ` WithPerceptionTracking ` , but if you forget
66+ then you will helpfully get a runtime warning letting you know that observation is not set up
67+ correctly:
6868
69- > 🟣 Runtime Warning: Perceptible state was accessed but is not being tracked. Track changes
70- > to state by wrapping your view in a 'WithPerceptionTracking' view. This must also be done
71- > for any escaping, trailing closures, such as 'GeometryReader', ` LazyVStack ` (and all lazy
72- > views), navigation APIs ('sheet', 'popover', 'fullScreenCover', etc.), and others.
69+ > 🟣 Runtime Warning: Perceptible state was accessed from a view but is not being tracked.
70+
71+ Finally, the ` Observations ` async sequence has been back-ported as ` Perceptions ` , which can be used
72+ to observe changes to perceptible and observable objects over time:
73+
74+ ``` swift
75+ let counts = Perceptions { model.count }
76+ for await count in counts {
77+ print (" Count changed: \( count ) " )
78+ }
79+ ```
7380
7481### Bindable
7582
76- SwiftUI's ` @Bindable ` property wrapper has also been backported to support perceptible objects. You
83+ SwiftUI's ` @Bindable ` property wrapper has also been back-ported to support perceptible objects. You
7784can simply qualify the property wrapper with the ` Perception ` module:
7885
7986``` swift
@@ -87,7 +94,7 @@ struct FeatureView: View {
8794### Environment
8895
8996SwiftUI's ` @Environment ` property wrapper and ` environment ` view modifier's support for observation
90- has also been backported to support perceptible objects using the exact same APIs:
97+ has also been back-ported to support perceptible objects using the exact same APIs:
9198
9299``` swift
93100struct FeatureView : View {
@@ -102,13 +109,14 @@ struct FeatureView: View {
102109
103110## Community
104111
105- If you want to discuss this library or have a question about how to use it to solve
106- a particular problem, there are a number of places you can discuss with fellow
112+ If you want to discuss this library or have a question about how to use it to solve a particular
113+ problem, there are a number of places you can discuss with fellow
107114[ Point-Free] ( https://www.pointfree.co ) enthusiasts:
108115
109- * For long-form discussions, we recommend the
110- [ discussions] ( https://github.com/pointfreeco/swift-perception/discussions ) tab of this repo.
111- * For casual chat, we recommend the [ Point-Free Community Slack] ( https://pointfree.co/slack-invite ) .
116+ * For long-form discussions, we recommend the
117+ [ discussions] ( https://github.com/pointfreeco/swift-perception/discussions ) tab of this repo.
118+ * For casual chat, we recommend the
119+ [ Point-Free Community Slack] ( https://pointfree.co/slack-invite ) .
112120
113121## Documentation
114122
@@ -119,6 +127,6 @@ The latest documentation for the Perception APIs is available [here][docs].
119127This library is released under the MIT license. See [ LICENSE] ( LICENSE ) for details.
120128
121129[ pointfreeco ] : https://www.pointfree.co
122- [ mbrandonw ] : https://twitter .com/mbrandonw
123- [ stephencelis ] : https://twitter .com/stephencelis
130+ [ mbrandonw ] : https://x .com/mbrandonw
131+ [ stephencelis ] : https://x .com/stephencelis
124132[ docs ] : https://swiftpackageindex.com/pointfreeco/swift-perception/main/documentation/perception
0 commit comments