replaceEmpty(with:)
Replaces an empty stream with the provided element.
Declaration
func replaceEmpty(with output: Self.Output) -> Publishers.ReplaceEmpty<Self>Parameters
- output:
An element to emit when the upstream publisher finishes without emitting any elements.
Return Value
A publisher that replaces an empty stream with the provided output element.
Discussion
Use replaceEmpty(with:) to provide a replacement element if the upstream publisher finishes without producing any elements.
In the example below, the empty Double array publisher doesn’t produce any elements, so replaceEmpty(with:) publishes Double.nan and finishes normally.
let numbers: [Double] = []
cancellable = numbers.publisher
.replaceEmpty(with: Double.nan)
.sink { print("\($0)", terminator: " ") }
// Prints "(nan)".Conversely, providing a non-empty publisher publishes all elements and the publisher then terminates normally:
let otherNumbers: [Double] = [1.0, 2.0, 3.0]
cancellable2 = otherNumbers.publisher
.replaceEmpty(with: Double.nan)
.sink { print("\($0)", terminator: " ") }
// Prints: 1.0 2.0 3.0