Skip to content

Dependency

tgrapperon edited this page Aug 31, 2021 · 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 ComposableDependencies, 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 ComposableDependencies {
  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: KeyPath<ComposableDependencies, Value>) 

Properties

wrappedValue

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

Clone this wiki locally