swift 运算符_Swift:您应该知道的9个筛选组合运算符

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

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

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

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

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

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

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

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

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

资源资源 (Resources)

The source code is available in a Gist.

源代码在Gist中可用。

结语 (Wrapping Up)

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

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

Thanks for reading!

谢谢阅读!

翻译自: https://levelup.gitconnected.com/9-filtering-combine-operators-you-should-know-9c1ef2911352

swift 运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值