A type-erasing cancellable object that executes a provided closure when canceled.


final class AnyCancellable


Subscriber implementations can use this type to provide a “cancellation token” that makes it possible for a caller to cancel a publisher, but not to use the Subscription object to request items.

An AnyCancellable instance automatically calls cancel() when deinitialized.


Creating a Type-Erased Cancellable

init(() -> Void)

Initializes the cancellable object with the given cancel-time closure.

Canceling Actions

Storing AnyCancellable Instances

func store<C>(in: inout C)

Stores this type-erasing cancellable instance in the specified collection.

func store(in: inout Set<AnyCancellable>)

Stores this type-erasing cancellable instance in the specified set.


Instance Properties


Conforms To

See Also


protocol Publisher

Declares that a type can transmit a sequence of values over time.

enum Publishers

A namespace for types that serve as publishers.

struct AnyPublisher

A publisher that performs type erasure by wrapping another publisher.

struct Published

A type that publishes a property marked with an attribute.

protocol Cancellable

A protocol indicating that an activity or action supports cancellation.