forked from sideeffect-io/AsyncExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncSequence+Assign.swift
More file actions
29 lines (28 loc) · 838 Bytes
/
AsyncSequence+Assign.swift
File metadata and controls
29 lines (28 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// AsyncSequence+Assign.swift
//
//
// Created by Thibault Wittemberg on 02/02/2022.
//
public extension AsyncSequence {
/// Assigns each element from the async sequence to a property on an object.
///
/// ```
/// class Root {
/// var property: String = ""
/// }
///
/// let root = Root()
/// let sequence = AsyncLazySequence(["1", "2", "3"])
/// try await sequence.assign(to: \.property, on: root) // will set the property value to "1", "2", "3"
/// ```
///
/// - Parameters:
/// - keyPath: A key path that indicates the property to assign.
/// - object: The object that contains the property.
func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root, Self.Element>, on object: Root) async throws {
for try await element in self {
object[keyPath: keyPath] = element
}
}
}