swift 运算符_Swift:您应该知道的5个转换组合运算符

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 accepting Int values.

    我们创建一个接受Int值的PassthroughSubject

  • We use the .map operator to transform each received Int value into a String.

    我们使用.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:

    我们向受试者发送随机数以观察以下结果:
Image for post

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属性。 然后我们只需打印每个国家:

Image for post

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:

我们得到想要的结果:

Image for post

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 )。 然后,一旦我们为其分配了新值,就会发生以下情况:

Image for post

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,然后强制解开值,最后,将所有值收集在一个数组中:

Image for post

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:

结果,我们计算出总收入:

Image for post

资源资源 (Resources)

The source code of an Xcode Playground is available in a gist.

Xcode Playground的源代码可以在gist中找到

结语 (Wrapping Up)

Want to learn more about Combine? Check out my other relevant articles:

想更多地了解合并? 查看其他相关文章:

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/5-transforming-combine-operators-you-should-know-4603fe112d74

swift 运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值