State
-
A binding is a reference to a value. It does not represent a source of truth, but it acts as a proxy being able to read and modify the original value. Reading it’s
See morewrappedValue
from a view’s body will trigger a subscription from the view to changes in the originally referenced value.Declaration
Swift
@propertyWrapper @dynamicMemberLookup public struct Binding<Value>
-
References and observes environment values set by the framework.
Currently supported environment values are:
- presentationMode
Declaration
Swift
@propertyWrapper public class Environment<Value> : DynamicProperty
-
Used to reference and observe environment objects previously set. Environment objects are identified by their type, meaning only one of the same type can exist at a time as an environment object.
Important
Referencing an environment object not previously set will trigger an exception.Declaration
Swift
@propertyWrapper public class EnvironmentObject<ObjectType> : DynamicProperty where ObjectType : ObservableObject
-
Used to observe an
See moreObservableObject
when changes to itsPublished
properties occur.Declaration
Swift
@propertyWrapper public class ObservedObject<ObjectType> where ObjectType : ObservableObject
-
A type that publishes a property.
When the wrapped value of a published type changes, views that have accessed to its parent
ObservableObject
through a@ObservedObject
property, will receive render updates.Important
Published types should be used only inObservableObject
classes.Declaration
Swift
@propertyWrapper public class Published<Value> : ChildWrapper
-
Represents a mutable value to be held by a
View
. Reading a state’swrappedValue
property inside a view’sbody
computed property will trigger a subscribption by the view to listen to changes in the state’s value.Important
You shouldn’t modify a state’s wrapped value when the view’sbody
property is being read.Declaration
Swift
@propertyWrapper public class State<Value>
-
StateObject works similarly as ObservedObject, with the only difference that for being part of a view’s state, if the view is recreated while owning an object, the object won’t be recreated and will keep it’s previous data.
On view recreation (by a state change in its parent):
- ObservedObject owned by view: Gets recreated (re-initialized)
- StateObject owned by view: Keeps same instance
Declaration
Swift
@propertyWrapper public class StateObject<ObjectType> where ObjectType : ObservableObject