Declares that a type can transmit a sequence of values over time.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
A publisher delivers elements to one or more
Subscriber instances. The subscriber’s
Failure associated types must match the
Failure types declared by the publisher. The publisher implements the
receive(subscriber:) method to accept a subscriber. After this, the publisher can call the following methods on the subscriber:
receive(subscription:): Acknowledges the subscribe request and returns a
Subscriptioninstance. The subscriber uses the subscription to demand elements from the publisher and can use it to cancel publishing.
receive(_:): Delivers one element from the publisher to the subscriber.
receive(completion:): Informs the subscriber that publishing has ended, either normally or with an error.
Publisher must adhere to this contract for downstream subscribers to function correctly.
Publisher define a wide variety of operators that you compose to create sophisticated event-processing chains. Each operator returns a type that implements the
Publisher protocol. Most of these types exist as extensions on the
Publishers enumeration. For example, the
map(_:) operator returns an instance of
Rather than implementing
Publisher on your own, you can use one of several types provided by the Combine framework. For example, you can use an AnySubject instance and publish new elements imperatively with its send(_:) method. You can also add the
@Published annotation to any property to give it a publisher that returns an instance of
Published, which emits an event every time the property’s value changes.