---
title: "replaceEmpty(with:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/replaceempty(with:)"
---

# replaceEmpty(with:)

Replaces an empty stream with the provided element.

## Declaration

```swift
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

Return Value A publisher that replaces an empty stream with the provided output element.

## Discussion

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

## See Also

### Filtering elements

- [filter(_:)](combine/publisher/filter(_:).md)
- [tryFilter(_:)](combine/publisher/tryfilter(_:).md)
- [compactMap(_:)](combine/publisher/compactmap(_:).md)
- [tryCompactMap(_:)](combine/publisher/trycompactmap(_:).md)
- [removeDuplicates()](combine/publisher/removeduplicates().md)
- [removeDuplicates(by:)](combine/publisher/removeduplicates(by:).md)
- [tryRemoveDuplicates(by:)](combine/publisher/tryremoveduplicates(by:).md)
- [replaceError(with:)](combine/publisher/replaceerror(with:).md)
