DependencyValues
A collection of dependencies that is globally available.
struct DependencyValues
To access a particular dependency from the collection you use the Dependency
property wrapper:
@Dependency(\.date) var date
// ...
let now = date.now
To change a dependency for a well-defined scope you can use the withDependencies(_:operation:)
method:
@Dependency(\.date) var date
let now = date.now
withDependencies {
$0.date.now = Date(timeIntervalSinceReferenceDate: 1234567890)
} operation: {
@Dependency(\.date.now) var now: Date
now.timeIntervalSinceReferenceDate // 1234567890
}
The dependencies will be changed for the lifetime of the operation
scope, which can be synchronous or asynchronous.
To register a dependency inside DependencyValues
, you first create a type to conform to the DependencyKey
protocol in order to specify the liveValue
to use for the dependency when run in simulators and on devices. It can even be private:
private enum MyValueKey: DependencyKey {
static let liveValue = 42
}
And then extend DependencyValues
with a computed property that uses the key to read and write to DependencyValues
:
extension DependencyValues {
var myValue: Int {
get { self[MyValueKey.self] }
set { self[MyValueKey.self] = newValue }
}
}
With those steps done you can access the dependency using the Dependency
property wrapper:
@Dependency(\.myValue) var myValue
myValue // 42
Read the article Registering dependencies for more information.
Creating and accessing values
``subscript(key:file:function:line:)
Overriding values
func withDependencies<R>((inout DependencyValues) throws -> Void, operation: () throws -> R
) rethrows -> R Updates the current dependencies for the duration of a synchronous operation.
withDependencies(from:operation:file:line:)-8e74m
Escaping contexts
func withEscapedDependencies<R>((DependencyValues.Continuation) throws -> R
) rethrows -> R Propagates the current dependencies to an escaping context.
Dependency values
var assert: any AssertionEffect
A dependency for handling assertions.
var assertionFailure: any AssertionFailureEffect
A dependency for failing an assertion.
var calendar: Calendar
The current calendar that features should use when handling dates.
var context: DependencyContext
The current dependency context.
var continuousClock: any Clock<Duration>
The current clock that features should use when a
ContinuousClock
would be appropriate.var date: DateGenerator
A dependency that returns the current date.
var fireAndForget: FireAndForget
A dependency for firing off an unstructured task.
var locale: Locale
var mainQueue: AnySchedulerOf<DispatchQueue>
The “main” queue.
var mainRunLoop: AnySchedulerOf<RunLoop>
The “main” run loop.
var openURL: OpenURLEffect
A dependency that opens a URL.
var precondition: any AssertionEffect
A dependency for handling preconditions.
var suspendingClock: any Clock<Duration>
The current clock that features should use when a
SuspendingClock
would be appropriate.var timeZone: TimeZone
The current time zone that features should use when handling dates.
var urlSession: URLSession
The URL session that features should use to make URL requests.
var uuid: UUIDGenerator
A dependency that generates UUIDs.
var withRandomNumberGenerator: WithRandomNumberGenerator
A dependency that yields a random number generator to a closure.
Default contexts
static var live: `Self`
A collection of “live” dependencies.
static var preview: `Self`
A collection of “preview” dependencies.
static var test: `Self`
A collection of “test” dependencies.
Deprecations
Deprecations
Review unsupported dependency values APIs and their replacements.
Read More