swift 运算符
In this article, we will explore five great Combine transforming operators that make it easy to change your data into a different format on the go.
在本文中,我们将探讨5种出色的Combine转换运算符,这些运算符使您可以随时随地轻松地将数据更改为其他格式。
We will do so by creating example functions in an Xcode Playground and running them to observe the results.
我们将通过在Xcode Playground中创建示例函数并运行它们以观察结果来做到这一点。
The source code is available at the bottom of the article.
源代码位于文章底部。
Let’s start.
开始吧。
1.地图 (1. map)
The .map
operator allows us to transform all elements from a publisher in a closure.
.map
运算符允许我们转换闭包中来自发布者的所有元素。
Here is what this code does:
这是这段代码的作用:
We create a
PassthroughSubject
acceptingInt
values.我们创建一个接受
Int
值的PassthroughSubject
。We use the
.map
operator to transform each receivedInt
value into aString
.我们使用
.map
运算符将每个接收到的Int
值转换为String
。- Then we subscribe to the publisher and print the transformed element’s value and type. 然后,我们订阅发布者并打印转换后的元素的值和类型。
- We send random numbers on to the subject to observe the following result: 我们向受试者发送随机数以观察以下结果:
We also have a neat way to obtain a property of an object using its key path:
我们还有一种巧妙的方法来使用对象的键路径来获取对象的属性:
As we can see, using .map(\.country)
, we access the country property of a CarBrand
. Then we simply print each country:
如我们所见,使用.map(\.country)
,我们访问CarBrand
的country属性。 然后我们只需打印每个国家:
2.收集 (2. collect)
The .collect
operator makes it easy to collect all received elements and emit a single array containing all of them:
.collect
运算符使收集变得容易 所有接收到的元素,并发出包含所有元素的单个数组:
We get the result we wanted:
我们得到想要的结果:
Note: The publisher must emit a .completed
event for this to happen, because .collect
is a greedy operator and waits until all elements are emitted and a publisher is finished.
注意:发布者必须发出.completed
事件才能发生这种情况,因为.collect
是一个贪婪的运算符,它会等待直到所有元素都发出并且发布者完成为止。
For example, if we were using a PassthroughSubject
, we would need to send a .finished
event after sending all the elements:
例如,如果我们使用PassthroughSubject
,则需要在发送所有元素之后发送一个.finished
事件:
3. flatMap (3. flatMap)
The .flatMap
operator allows us to transform a given publisher into another one. Take a look at how we change the observation from a Network
to its isAvailable
subject:
.flatMap
运算符允许我们将给定的发布者转换为另一个发布者。 看看我们如何将观察结果从Network
更改为isAvailable
主题:
As a result, we have the isAvailable
value printed out as we change its value. First it prints the initial value (we are using a CurrentValueSubject
). Then once we assign a new value to it, the following occurs:
结果,当我们更改其值时,我们将打印出isAvailable
值。 首先,它打印初始值(我们正在使用CurrentValueSubject
)。 然后,一旦我们为其分配了新值,就会发生以下情况:
4. replaceNil (4. replaceNil)
As its name suggests, the .replaceNil
operator transforms each received nil
element into one we specify:
顾名思义, .replaceNil
运算符将接收到的每个nil
元素转换为我们指定的元素:
Note that we can also combine several operators together to achieve the necessary result. First we replace each nil
value with 0, then we force unwrap values, and finally, we collect all of them in a single array:
请注意,我们还可以将多个运算符组合在一起以达到必要的结果。 首先,我们将每个nil
值替换为0,然后强制解开值,最后,将所有值收集在一个数组中:
Note how we use force unwrapping inside a .map
operator. What to do if you don’t like force unwrap? For this, we have a co-variant of .map
: .compactMap
. When we use it, it automatically forwards only those elements that are not nil:
注意我们如何在.map
运算符内使用强制展开。 如果您不喜欢强制解包怎么办? 为此,我们有.map
的协变量: .compactMap
。 当我们使用它时,它将自动转发仅非零的那些元素:
5.扫描 (5. scan)
The .scan
operator exposes the currently emitted value, along with the latest one, in a closure. We can use it to accumulate values and print a total result:
.scan
运算符在闭包中公开当前发出的值以及最新的值。 我们可以使用它来累积值并打印总结果:
Here we do the following:
在这里,我们执行以下操作:
- Create an array of earnings (underscore is a nice way to separate the thousand part in a number). 创建收益数组(下划线是将数字中的千分之一分开的好方法)。
- Create a publisher of these earnings. 创建这些收入的发布者。
Using the
.scan
operator, add the currently emitted value ($0) to the latest one ($1), starting from zero.使用
.scan
运算符,将当前发出的值($ 0)添加到.scan
的最新值($ 1)。
As a result, we compute the total earnings:
结果,我们计算出总收入:
资源资源 (Resources)
The source code of an Xcode Playground is available in a gist.
Xcode Playground的源代码可以在gist中找到 。
翻译自: https://medium.com/better-programming/5-transforming-combine-operators-you-should-know-4603fe112d74
swift 运算符