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
Creating Your Own Publishers
Rather than implementing the
Publisher protocol yourself, you can create your own publisher by using one of several types provided by the Combine framework:
Currentto publish whenever you update the subject’s underlying value.
@Publishedannotation to a property of one of your own types. In doing so, the property gains a publisher that emits an event whenever the property’s value changes. See the
Publishedtype for an example of this approach.