文章以图文的形式,记录 Combine 中各操作符的用法以及效果。下文为原文章内容的转载 + 翻译。

本文转载自 Cheat sheet on s for iOS development
点击查看原文以更好的效果查看内容。


allSatisfy(_:)

Publishes true if all elements match the condition. Publishes false & completes if it receives an element that does not match the condition.

如果所有的元素都符合条件,则返回 true。如果收到一个不符合条件的元素,则返回 false 并完成信号。

append(_:)

Appends the specified elements after the upstream publisher completes.

在上游发布者完成后添加指定的元素。

collect(_:)

Collects up to the specified number of elements and emits them in an array.

收集多达指定数量的元素,并将它们发射到一个数组中。

combineLatest(_:)

Publishes a tuple of the most recent element from multiple publishers when any of them emits a value.

当任何一个发布者发出一个值时,发布一个来自多个发布者的最新元素的元组。

compactMap(_:)

Transforms each element with the provided closure and publishes any returned non-nil optional result.

用提供的闭包对每个元素进行转换,并公布任何返回的非零的可选结果。

contains(_:)

Publishes true & completes after receiving a matching value, or false when upstream publisher completes without producing a matching value.

在收到一个匹配的值后发布真值&完成,或者在上游发布者完成后没有产生一个匹配的值时发布假值。

count()

Publishes the count of all received elements when the upstream publisher completes.

当上游发布器完成时,发布所有收到的元素的计数。

dropFirst(_:)

Omits a specified number of elements, then republishes all remaining elements.

省略指定数量的元素,然后重新发布所有剩余的元素。

drop(while:)

Omits elements until a given closure returns false, then republishes all remaining elements.

省略元素,直到一个给定的闭包返回错误,然后重新发布所有剩余的元素。

filter(_:)

Republishes all elements that match a provided closure.

重新发布所有符合所提供的闭包的元素。

first()

Publishes the first element of the upstream publisher, then completes.

发布上游发布器的第一个元素,然后完成。

first(where:)

Publishes the first element of a stream that satisfies a condition, then completes.

发布一个满足条件的流的第一个元素,然后完成。

flatMap(maxPublishers:_:)

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

将上游发布器中的所有元素转化为一个新的发布器。

ignoreOutput()

Ignores all elements and passes along only the upstream publisher's completion state.

忽略所有元素,只传递上游发布者的完成状态。

last()

Publishes the last element of the upstream publisher after it completes.

在上游发布器完成后,发布它的最后一个元素。

last(where:)

Publishes the last element that satisfies a condition after the upstream publisher completes.

在上游发布器完成后,发布满足条件的最后一个元素。

map(_:)

Transforms each element it receives from the upstream publisher with the provided closure.

用所提供的闭包对它从上游发布者那里收到的每个元素进行转换。

max()

Publishes the maximum value received after the upstream publisher completes.

在上游发布者完成后,发布收到的最大值。

merge(with:)

Publishes an element whenever any of the upstream publishers emits an element.

每当任何一个上游发布者发出一个元素,就发布一个元素。

min()

Publishes the minimum value received after the upstream publisher completes.

发布上游发布者完成后收到的最小值。

output(at:)

Republishes an element specified by its position, or completes if upstream publisher completes before the specified element.

重新发布一个由其位置指定的元素,如果上游发布者在指定的元素之前完成,则完成。

output(in:)

Republishes elements specified by a range of positions.

重新发布由某个位置范围指定的元素。

prefix(_:)

Republishes elements up to a specified maximum count.

重新发布元素,直到指定的最大数量。

prefix(while:)

Emits values while elements meet the specified condition, otherwise it completes.

当元素满足指定的条件时发出数值,否则就会完成。

prepend(_:)

Prepends elements with the specified values.

用指定的值对元素进行预置。

reduce(_:_:)

Applies a closure to all elements and publishes a final result when the upstream publisher completes.

对所有元素应用一个闭包,并在上游发布器完成后发布一个最终结果。

removeDuplicates()

Publishes an element only if it doesn't match the previous element.

只在一个元素与前一个元素不匹配时才发布。

replaceNil(with:)

Replaces nil elements with the provided element.

用所提供的元素替换nil元素。

scan(_:_:)

Transforms elements by applying a closure that receives its previous return value and the next element from the upstream publisher.

通过应用一个闭包来转换元素,该闭包从上游发布者那里接收其先前的返回值和下一个元素。

switchToLatest()

Republishes elements sent by the most recently received publisher.

重新发布由最近收到的发布者发送的元素。

zip(_:)

Waits until both publishers emit an element, then publishes the oldest unconsumed element from each one as a tuple.

等待,直到两个发布者都发出一个元素,然后将每个发布者中最古老的未被吸收的元素作为一个元组发布。