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

# replaceNil(with:)

Replaces nil elements in the stream with the provided element.

## Declaration

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

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

## Discussion

Discussion The 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:) 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)"

## See Also

### Mapping elements

- [map(_:)](combine/publisher/map(_:)-99evh.md)
- [tryMap(_:)](combine/publisher/trymap(_:).md)
- [mapError(_:)](combine/publisher/maperror(_:).md)
- [scan(_:_:)](combine/publisher/scan(_:_:).md)
- [tryScan(_:_:)](combine/publisher/tryscan(_:_:).md)
- [setFailureType(to:)](combine/publisher/setfailuretype(to:).md)
