<!--
{
  "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/replaceNil(with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Combine"
    ],
    "preciseIdentifier" : "s:7Combine9PublisherPAAE10replaceNil4withAA10PublishersO3MapVy_xqd__Gqd___tqd__Sg6OutputRtzlF"
  },
  "title" : "replaceNil(with:)"
}
-->

# replaceNil(with:)

Replaces nil elements in the stream with the provided element.

```
func replaceNil<T>(with output: T) -> Publishers.Map<Self, T> where Self.Output == T?
```

## Parameters

`output`

The element to use when replacing `nil`.

## Return Value

A publisher that replaces `nil` elements from the upstream publisher with the provided element.

## Discussion

The [`replaceNil(with:)`](/documentation/Combine/Publisher/replaceNil(with:)) operator enables replacement of `nil` values in a stream with a substitute value. In the example below, a collection publisher contains a nil value. The [`replaceNil(with:)`](/documentation/Combine/Publisher/replaceNil(with:)) operator replaces this with `0.0`.

```
let numbers: [Double?] = [1.0, 2.0, nil, 3.0]
numbers.publisher
    .replaceNil(with: 0.0)
    .sink { print("\($0)", terminator: " ") }

// Prints: "Optional(1.0) Optional(2.0) Optional(0.0) Optional(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)