moreswift/swift-observation-polyfill
Observation was introduced in iOS 17.0, macOS 14.0, tvOS 17.0, visionOS 1.0, and watchOS 10.0. This polyfill brings the Observation API to iOS 13.0, macOS 10.15, tvOS 13.0, and watchOS 6.0.
Important information
- Observable models produced using the polyfill are compatible with APIs that expect real
Observation.Observablemodels. This is because the polyfilled macro generates a conformance toObservation.ObservablewhenObservationis available. - Observable models produced using
Observationare not compatible with APIs that expect polyfilledObservationPolyfill.Observablemodels. - If your deployment target is lower than the platform version that brought the
Observationlibrary, then you can always use this polyfill's declarations unqualified (i.e.@Observableinstead of@ObservationPolyfill.Observable), even ifObservation/SwiftUIis in scope. @Observableunambiguously refers to theObservationPolyfill.Observablemacro as long as your deployment target is low enough thatObservation.Observableisn't unconditionally available.- Library developers relying on
ObservationPolyfillmust implement special handling and/or multiple copies of their APIs if they would like users to be able to use both polyfilled models and realObservation.Observablemodels with their library. This is because users with high enough deployment targets may want to be able to just use the first-partyObservationmodule, especially when porting existing code from SwiftUI etc. - When using
ObservationPolyfillwith SwiftUI and targeting pre-Observation OS versions, you must wrap each view body in theWithObservationTrackingview to ensure that state tracking functions correctly. It's easy to forget to do this if you only test your app on the latest OS version, because your app will appear to work fine both with and withoutWithObservationTracking. This is the same way that things work when using swift-perception.
In short, there generally aren't any drawbacks to using ObservationPolyfill as an application developer, because polyfilled models are still compatible with regular Observation APIs, but as a library developer you have to take more care to support both Observation and ObservationPolyfill.
Package Metadata
Repository: moreswift/swift-observation-polyfill
Default branch: main
README: README.md