StateObject

@propertyWrapper
public class StateObject<ObjectType> where ObjectType : ObservableObject

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
  • A wrapper of the underlying ObservableObject that can create Bindings to its properties using dynamic member lookup.

    See more

    Declaration

    Swift

    @dynamicMemberLookup
    public struct Wrapper
  • Declaration

    Swift

    public init(wrappedValue value: ObjectType)
  • The internal value of this wrapper type.

    Declaration

    Swift

    public var wrappedValue: ObjectType { get set }
  • The direct value of this wrapper, as accessing $foo on a @EnvironmentObject property.

    Declaration

    Swift

    public var projectedValue: StateObject<ObjectType>.Wrapper { get }