swift 运算符
In this article we will explore and master 9 useful filtering Combine operators in an Xcode Playground.
在本文中,我们将探索和掌握Xcode Playground中9个有用的过滤Combine运算符。
The source code is available at the bottom of the article.
源代码位于文章底部。
开始吧 (Let’s Start)
1)过滤器 (1) filter)
We use the .filter
operator if we want to accept only those elements that match a certain condition:
如果只想接受符合特定条件的元素,则使用.filter
运算符:
Here we print only those String
values that contain the “o” letter and ignore the others:
在这里,我们仅打印那些包含“ o”字母的String
值,而忽略其他String
:
![Image for post](https://miro.medium.com/max/9999/1*KxGXNuGsoNAGzXGXQwZEUw.png)
2)removeDuplicates (2) removeDuplicates)
The .removeDuplicates
operator, as its name implies, allows us to obtain only unique values:
顾名思义, .removeDuplicates
运算符使我们只能获取唯一值:
As a result, we have the following integers printed out:
结果,我们打印出以下整数:
![Image for post](https://miro.medium.com/max/9999/1*eOPuLniIVHBJwNvdDWpYVA.png)
3)compactMap (3) compactMap)
If you ever had to deal with handling Optionals in reactive programming, then you will probably appreciate the .compactMap
operator. It allows us to obtain only non-nil values:
如果您不得不处理React式编程中的Optionals,那么您可能会喜欢.compactMap
运算符。 它允许我们仅获取非nil值:
As we can see, the transformation of “Hello World” to Int
returns nil
, so we don’t see it printed in the console:
如我们所见,“ Hello World”到Int
返回nil
,因此我们看不到它在控制台中打印:
![Image for post](https://miro.medium.com/max/9999/1*1WOhhAp6ZYGN5HenC5L1XQ.png)
4)ignoreOutput (4) ignoreOutput)
As its name implies, the .ignoreOutput
operator ignores all upstream events. However, it passes along a completion state (finished or failed). It is useful in cases when you don’t care about values and only need to know if a publisher has finished:
顾名思义, .ignoreOutput
运算符将忽略所有上游事件。 但是,它经过完成状态(完成或失败)。 在您不关心值而只需要知道发布者是否完成的情况下,它很有用:
We can see that only the completion event is printed:
我们可以看到只打印完成事件:
![Image for post](https://miro.medium.com/max/9999/1*Mf3beVLgBnKZ-7Bu7IKIsQ.png)
5)首先(其中:) (5) first(where:))
We use the .first(where:)
operator to obtain the first value that matches our criteria in a subscription:
我们使用.first(where:)
运算符来获取与订阅中的条件匹配的第一个值:
We can see that “lorem” and “ipsum” begin with a lowercase character. The “Dolor” string satisfies the criteria, so it is printed out and the publisher finishes:
我们可以看到“ lorem”和“ ipsum”以小写字母开头。 “ Dolor”字符串满足条件,因此将其打印出来,并由发布者完成:
![Image for post](https://miro.medium.com/max/9999/1*5h9shLV9z-zVML8fKCU0Lg.png)
6)dropFirst(计数:) (6) dropFirst(count:))
The .dropFirst(count:)
operator allows us to disregard a number of elements we pass as a parameter:
.dropFirst(count:)
运算符使我们可以忽略作为参数传递的许多元素:
As a result, we have only “Hello World” and “5” printed in the console:
结果,控制台中仅打印了“ Hello World”和“ 5”:
![Image for post](https://miro.medium.com/max/9999/1*FsxtbU2tvg_gwyXEuNPn_w.png)
7)丢弃(while :) (7) drop(while:))
This operator disregards elements until the given predicate is not satisfied:
此运算符将忽略元素,直到不满足给定谓词为止:
Here we omit elements until we have a String
having fewer than three characters. In our case, it is “Sit”, therefore, “Sit” and “amet” are printed:
在这里,我们省略元素,直到拥有少于三个字符的String
为止。 在我们的例子中,它是“ Sit”,因此,将打印“ Sit”和“ amet”:
![Image for post](https://miro.medium.com/max/9999/1*3a874Or8OVOZ3nUISHLmoQ.png)
8)drop(直到untilOutputFrom :) (8) drop(untilOutputFrom:))
We use this version of the .drop
operator, when we want to have a “trigger” publisher that allows the source publisher to emit values. Once a value is sent onto the “trigger” publisher, the source publisher is allowed to execute:
当我们希望有一个“触发”发布者允许源发布者发出值时,我们使用此版本的.drop
运算符。 将值发送到“触发器”发布者后,源发布者便可以执行:
Here we create a Timer publisher, that is allowed to emit events once the startTrigger
subject receives a signal. We send a Void
value onto the startTrigger
subject after three seconds, so we have the Timer starting to print values only from the fourth second:
在这里,我们创建了一个Timer发布器,一旦startTrigger
主题接收到信号,就可以发出事件。 三秒钟后,我们将Void
值发送到startTrigger
主题,因此,Timer仅从第四秒开始打印值:
![Image for post](https://miro.medium.com/max/9999/1*NwIDPoIZrzC4gLCN04TBOA.png)
9).prefix(untilOutputFrom :) (9) .prefix(untilOutputFrom:))
The last operator of this article is the opposite of .drop(untilOutputFrom:)
. We stop receiving values from the source publisher once the trigger publisher receives a value:
本文的最后一个运算符与.drop(untilOutputFrom:)
相反。 一旦触发器发布者收到一个值,我们将停止从源发布者那里接收值:
As a result, the Timer prints three seconds and immediately stops once we send a value onto the stopTrigger
subject:
结果,Timer打印了三秒钟,并在我们将值发送到stopTrigger
主题后立即停止:
![Image for post](https://miro.medium.com/max/9999/1*sqxxy7ODvozNlO7wksKEqQ.png)
翻译自: https://levelup.gitconnected.com/9-filtering-combine-operators-you-should-know-9c1ef2911352
swift 运算符