<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Combine",
  "identifier" : "/documentation/Combine/Publisher/replaceEmpty(with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE12replaceEmpty4withAA10PublishersO07ReplaceD0Vy_xG6OutputQz_tF"
  },
  "title" : "replaceEmpty(with:)"
}
-->

# replaceEmpty(with:)

Replaces an empty stream with the provided element.

```
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:)`](/documentation/Combine/Publisher/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:)`](/documentation/Combine/Publisher/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
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)