Generic Structure

Publishers.Optional

A publisher that publishes an optional value to each subscriber exactly once, if the optional has a value.

Declaration

struct Optional<Output, Failure> where Failure : Error

Overview

If result is .success, and the value is non-nil, then Optional waits until receiving a request for at least 1 value before sending the output. If result is .failure, then Optional sends the failure immediately upon subscription. If result is .success and the value is nil, then Optional sends .finished immediately upon subscription.

In contrast with Just, an Optional publisher can send an error. In contrast with Once, an Optional publisher can send zero values and finish normally, or send zero values and fail with an error.

Topics

Initializers

init(Result<Output?, Failure>)

Creates a publisher to emit the optional value of a successful result, or fail with an error.

Instance Properties

let result: Result<Output?, Failure>

The result to deliver to each subscriber.

Instance Methods

func append(Output...) -> Publishers.Concatenate<Publishers.Optional<Output, Failure>, Publishers.Sequence<[Output], Failure>>

Append a Publisher’s output with the specified sequence.

func assertNoFailure(String, file: StaticString, line: UInt) -> Publishers.AssertNoFailure<Publishers.Optional<Output, Failure>>

Raises a fatal error when its upstream publisher fails, and otherwise republishes all received input.

func assign<Root>(to: ReferenceWritableKeyPath<Root, Output>, on: Root) -> AnyCancellable

Assigns the value of a KVO-compliant property from a publisher.

func breakpointOnError() -> Publishers.Breakpoint<Publishers.Optional<Output, Failure>>

Raises a debugger signal upon receiving a failure.

func `catch`<P>((Failure) -> P) -> Publishers.Catch<Publishers.Optional<Output, Failure>, P>

Handles errors from an upstream publisher by replacing it with another publisher.

func collect(Int) -> Publishers.CollectByCount<Publishers.Optional<Output, Failure>>

Collects up to the specified number of elements, and then emits a single array of the collection.

func collect<S>(Publishers.TimeGroupingStrategy<S>, options: S.SchedulerOptions?) -> Publishers.CollectByTime<Publishers.Optional<Output, Failure>, S>

Collects elements by a given strategy, and emits a single array of the collection.

func compactMap<T>((Output) -> T?) -> Publishers.CompactMap<Publishers.Optional<Output, Failure>, T>

Calls a closure with each received element and publishes any returned optional that has a value.

func contains(Output) -> Publishers.Optional<Bool, Failure>
func contains(Output) -> Publishers.Contains<Publishers.Optional<Output, Failure>>

Publishes a Boolean value upon receiving an element equal to the argument.

func debounce<S>(for: S.SchedulerTimeType.Stride, scheduler: S, options: S.SchedulerOptions?) -> Publishers.Debounce<Publishers.Optional<Output, Failure>, S>

Publishes elements only after a specified time interval elapses between events.

func decode<Item, Coder>(type: Item.Type, decoder: Coder) -> Publishers.Decode<Publishers.Optional<Output, Failure>, Item, Coder>

Decodes the output from upstream using a specified TopLevelDecoder. For example, use JSONDecoder.

func delay<S>(for: S.SchedulerTimeType.Stride, tolerance: S.SchedulerTimeType.Stride?, scheduler: S, options: S.SchedulerOptions?) -> Publishers.Delay<Publishers.Optional<Output, Failure>, S>

Delays delivery of all output to the downstream receiver by a specified amount of time on a particular scheduler.

func drop<P>(untilOutputFrom: P) -> Publishers.DropUntilOutput<Publishers.Optional<Output, Failure>, P>

Ignores elements from the upstream publisher until it receives an element from a second publisher.

func encode<Coder>(encoder: Coder) -> Publishers.Encode<Publishers.Optional<Output, Failure>, Coder>

Encodes the output from upstream using a specified TopLevelEncoder. For example, use JSONEncoder.

func flatMap<T, P>(maxPublishers: Subscribers.Demand, (Output) -> P) -> Publishers.FlatMap<P, Publishers.Optional<Output, Failure>>

Transforms all elements from an upstream publisher into a new or existing publisher.

func makeConnectable() -> Publishers.MakeConnectable<Publishers.Optional<Output, Failure>>

Creates a connectable wrapper around the publisher.

func map<T>((Output) -> T) -> Publishers.Map<Publishers.Optional<Output, Failure>, T>

Transforms all elements from the upstream publisher with a provided closure.

func mapError<E>((Failure) -> E) -> Publishers.MapError<Publishers.Optional<Output, Failure>, E>

Converts any failure from the upstream publisher into a new error.

func max() -> Publishers.Optional<Output, Failure>
func max() -> Publishers.Comparison<Publishers.Optional<Output, Failure>>

Publishes the maximum value received from the upstream publisher, after it finishes.

func measureInterval<S>(using: S, options: S.SchedulerOptions?) -> Publishers.MeasureInterval<Publishers.Optional<Output, Failure>, S>

Measures and emits the time interval between events received from an upstream publisher.

func merge(with: Publishers.Optional<Output, Failure>) -> Publishers.MergeMany<Publishers.Optional<Output, Failure>>

Combines elements from this publisher with those from another publisher of the same type, delivering an interleaved sequence of elements.

func merge<B, C>(with: B, C) -> Publishers.Merge3<Publishers.Optional<Output, Failure>, B, C>

Combines elements from this publisher with those from two other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D>(with: B, C, D) -> Publishers.Merge4<Publishers.Optional<Output, Failure>, B, C, D>

Combines elements from this publisher with those from three other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E>(with: B, C, D, E) -> Publishers.Merge5<Publishers.Optional<Output, Failure>, B, C, D, E>

Combines elements from this publisher with those from four other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F>(with: B, C, D, E, F) -> Publishers.Merge6<Publishers.Optional<Output, Failure>, B, C, D, E, F>

Combines elements from this publisher with those from five other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F, G>(with: B, C, D, E, F, G) -> Publishers.Merge7<Publishers.Optional<Output, Failure>, B, C, D, E, F, G>

Combines elements from this publisher with those from six other publishers, delivering an interleaved sequence of elements.

func merge<B, C, D, E, F, G, H>(with: B, C, D, E, F, G, H) -> Publishers.Merge8<Publishers.Optional<Output, Failure>, B, C, D, E, F, G, H>

Combines elements from this publisher with those from seven other publishers, delivering an interleaved sequence of elements.

func min() -> Publishers.Optional<Output, Failure>
func min() -> Publishers.Comparison<Publishers.Optional<Output, Failure>>

Publishes the minimum value received from the upstream publisher, after it finishes.

func output<R>(in: R) -> Publishers.Output<Publishers.Optional<Output, Failure>>

Publishes elements specified by their range in the sequence of published elements.

func prefix<P>(untilOutputFrom: P) -> Publishers.PrefixUntilOutput<Publishers.Optional<Output, Failure>, P>

Republishes elements until another publisher emits an element.

func prepend(Output...) -> Publishers.Concatenate<Publishers.Sequence<[Output], Failure>, Publishers.Optional<Output, Failure>>

Prefixes a Publisher’s output with the specified sequence.

func print(String, to: TextOutputStream?) -> Publishers.Print<Publishers.Optional<Output, Failure>>

Prints log messages for all publishing events.

func receive<S>(on: S, options: S.SchedulerOptions?) -> Publishers.ReceiveOn<Publishers.Optional<Output, Failure>, S>

Specifies the scheduler on which to receive elements from the publisher.

func reduce<T>(T, (T, Output) -> T) -> Publishers.Reduce<Publishers.Optional<Output, Failure>, T>

Applies a closure that accumulates each element of a stream and publishes a final result upon completion.

func removeDuplicates() -> Publishers.Optional<Output, Failure>
func removeDuplicates() -> Publishers.RemoveDuplicates<Publishers.Optional<Output, Failure>>

Publishes only elements that don’t match the previous element.

func replaceNil<T>(with: T) -> Publishers.Map<Publishers.Optional<Output, Failure>, T>

Replaces nil elements in the stream with the proviced element.

func scan<T>(T, (T, Output) -> T) -> Publishers.Scan<Publishers.Optional<Output, Failure>, T>

Transforms elements from the upstream publisher by providing the current element to a closure along with the last value returned by the closure.

func setFailureType<E>(to: E.Type) -> Publishers.SetFailureType<Publishers.Optional<Output, Failure>, E>

Changes the failure type declared by the upstream publisher.

func share() -> Publishers.Share<Publishers.Optional<Output, Failure>>

Returns a publisher as a class instance.

func subscribe<S>(S) -> AnyCancellable

Attaches the specified Subscriber to this Publisher.

func subscribe<S>(S)

Attaches the specified subscriber to this publisher.

func subscribe<S>(on: S, options: S.SchedulerOptions?) -> Publishers.SubscribeOn<Publishers.Optional<Output, Failure>, S>

Specifies the scheduler on which to perform subscribe, cancel, and request operations.

func switchToLatest() -> Publishers.SwitchToLatest<Output, Publishers.Optional<Output, Failure>>

Flattens the stream of events from multiple upstream publishers to appear as if they were coming from a single stream of events.

func throttle<S>(for: S.SchedulerTimeType.Stride, scheduler: S, latest: Bool) -> Publishers.Throttle<Publishers.Optional<Output, Failure>, S>

Publishes either the most-recent or first element published by the upstream publisher in the specified time interval.

func timeout<S>(S.SchedulerTimeType.Stride, scheduler: S, options: S.SchedulerOptions?, customError: (() -> Failure)?) -> Publishers.Timeout<Publishers.Optional<Output, Failure>, S>

Terminates publishing if the upstream publisher exceeds the specified time interval without producing an element.

func tryCompactMap<T>((Output) -> T?) -> Publishers.TryCompactMap<Publishers.Optional<Output, Failure>, T>

Calls an error-throwing closure with each received element and publishes any returned optional that has a value.

func tryMap<T>((Output) -> T) -> Publishers.TryMap<Publishers.Optional<Output, Failure>, T>

Transforms all elements from the upstream publisher with a provided error-throwing closure.

func tryReduce<T>(T, (T, Output) -> T) -> Publishers.TryReduce<Publishers.Optional<Output, Failure>, T>

Applies an error-throwing closure that accumulates each element of a stream and publishes a final result upon completion.

func tryScan<T>(T, (T, Output) -> T) -> Publishers.TryScan<Publishers.Optional<Output, Failure>, T>

Transforms elements from the upstream publisher by providing the current element to an error-throwing closure along with the last value returned by the closure.

func zip<P>(P) -> Publishers.Zip<Publishers.Optional<Output, Failure>, P>

Combine elements from another publisher and deliver pairs of elements as tuples.

func zip<P, Q>(P, Q) -> Publishers.Zip3<Publishers.Optional<Output, Failure>, P, Q>

Combine elements from two other publishers and deliver groups of elements as tuples.

func zip<P, Q, R>(P, Q, R) -> Publishers.Zip4<Publishers.Optional<Output, Failure>, P, Q, R>

Combine elements from three other publishers and deliver groups of elements as tuples.

Relationships

Conforms To

  • Equatable

See Also

Convenience Publishers

struct Publishers.Empty

A publisher that never publishes any values, and optionally finishes immediately.

Beta
struct Publishers.Fail

A publisher that immediately terminates with the specified error.

Beta
struct Publishers.Once

A publisher that publishes an output to each subscriber exactly once then finishes, or fails immediately without producing any elements.

Beta
struct Publishers.Sequence

A publisher that publishes a given sequence of elements.

Beta
struct Publishers.Deferred

A publisher that awaits subscription before running the supplied closure to create a publisher for the new subscriber.

Beta

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software