prefix(_:)
Republishes elements up to the specified maximum count.
Declaration
func prefix(_ maxLength: Int) -> Publishers.Output<Self>Parameters
- maxLength:
The maximum number of elements to republish.
Return Value
A publisher that publishes up to the specified number of elements.
Discussion
Use prefix(_:) to limit the number of elements republished to the downstream subscriber.
In the example below, the prefix(_:) operator limits its output to the first two elements before finishing normally:
let numbers = (0...10)
cancellable = numbers.publisher
.prefix(2)
.sink { print("\($0)", terminator: " ") }
// Prints: "0 1"