Contents

count()

Publishes the number of elements received from the upstream publisher.

Declaration

func count() -> Publishers.Count<Self>

Return Value

A publisher that consumes all elements until the upstream publisher finishes, then emits a single value with the total number of elements received.

Discussion

Use count() to determine the number of elements received from the upstream publisher before it completes:

let numbers = (0...10)
cancellable = numbers.publisher
    .count()
    .sink { print("\($0)") }

// Prints: "11"

See Also

Applying mathematical operations on elements