Skip to content

Dependency

tgrapperon edited this page Aug 22, 2022 · 7 revisions

Dependency

Use this property wrapper to declare depencies in a ComposableEnvironment subclass.

@propertyWrapper
public struct Dependency<Value> 

You reference the dependency by its KeyPath originating from Dependencies, and you declare its name in the local environment. The dependency should not be instantiated, as it is either inherited from a ComposableEnvironment parent, or installed with ComposableEnvironment/with(_:_:).

For example, if the dependency is declared as:

extension Dependencies {
  var uuidGenerator: () -> UUID {
    get { self[UUIDGeneratorKey.self] }
    set { self[UUIDGeneratorKey.self] = newValue }
  }
},

you can install it in LocalEnvironment like:

class LocalEnvironment: ComposableEnvironment {
  @Dependency(\.uuidGenerator) var uuid
}

This exposes a var uuid: () -> UUID read-only property in the LocalEnvironment. This property can then be used as any vanilla dependency.

Initializers

init(_:)

See Dependency discussion

public init(_ keyPath: WritableKeyPath<Dependencies, Value>) 

init(_:)

@available(
    *, unavailable,
    message:
      """
  @Dependency should be used in conjunction with a `WritableKeyPath`. Please implement a setter
  part in the `Dependencies`'s computed property for this dependency.
  """
  )
  public init(_ keyPath: KeyPath<Dependencies, Value>) 

init(_:)

See Dependency discussion

public init(_ keyPath: KeyPath<Dependencies, Value>) 

Properties

wrappedValue

@available(
    *, unavailable, message: "@Dependency should be used in a ComposableEnvironment class."
  )
  public var wrappedValue: Value 

wrappedValue

public var wrappedValue: Value 
Clone this wiki locally