Contents

AnyPublisher

A publisher that performs type erasure by wrapping another publisher.

Declaration

@frozen struct AnyPublisher<Output, Failure> where Failure : Error

Mentioned in

Overview

AnyPublisher is a concrete implementation of Publisher that has no significant properties of its own, and passes through elements and completion values from its upstream publisher.

Use AnyPublisher to wrap a publisher whose type has details you don’t want to expose across API boundaries, such as different modules. Wrapping a Subject with AnyPublisher also prevents callers from accessing its send(_:) method. When you use type erasure this way, you can change the underlying publisher implementation over time without affecting existing clients.

You can use Combine’s eraseToAnyPublisher() operator to wrap a publisher with AnyPublisher.

Topics

Creating a type-erased publisher

See Also

Publishers