map(_:)
Publishes the value of a key path.
Declaration
func map<T>(_ keyPath: KeyPath<Self.Output, T>) -> Publishers.MapKeyPath<Self, T>Parameters
- keyPath:
The key path of a property on
Output.
Return Value
A publisher that publishes the value of the key path.
Discussion
In the following example, the map(_:) operator uses the Swift key path syntax to access the die member of the DiceRoll structure published by the Just publisher.
The downstream sink subscriber receives only the value of this Int, not the entire DiceRoll.
struct DiceRoll {
let die: Int
}
cancellable = Just(DiceRoll(die:Int.random(in:1...6)))
.map(\.die)
.sink {
print ("Rolled: \($0)")
}
// Prints "Rolled: 3" (or some other random value).