当前位置: 首页>移动开发>正文

swift 订阅 swift订阅付费功能开发

swift 订阅



In this article, we’ll learn how to create our own Combine subscriber to encapsulate the value- and completion-handling logic into a separate class.

在本文中,我们将学习如何创建自己的Combine订阅服务器,以将值处理和完成处理逻辑封装到一个单独的类中。

The source code of an Xcode Playground implementation is available at the bottom of the article.

本文底部提供了Xcode Playground实现的源代码。

(Let’s Start)

Say our goal is to receive string values and print an uppercase version of them.

说我们的目标是接收字符串值并打印它们的大写版本。

First, we need to create a StringSubscriber class conforming to the Subscriber protocol:

首先,我们需要创建一个符合Subscriber协议的StringSubscriber类:



Now we must provide two type aliases, Input and Failure. Input stands for the type of the values we receive from publishers. In our case, it’s String. We don’t want to receive any Errors, so we set the Failure to Never:

现在,我们必须提供两个类型别名, InputFailureInput代表我们从发布者那里收到的值的类型。 在我们的例子中,它是String 。 我们不想收到任何Error ,因此我们将Failure设置为Never



We have to implement three required methods:

我们必须实现三种必需的方法:

  • One that specifies the number of values our subscriber can receive
  • One that handles the received input and expands the number of values the subscriber can receive
  • One that handles the completion event

First, let’s specify the the maximum number of future values:

首先,让我们指定将来值的最大数量:



We have no limitation for the count of Strings we’d receive, so we specify an .unlimited enum case.

我们对收到的String的数量没有限制,因此我们指定了.unlimited枚举大小写。

Now let’s handle the input:

现在让我们处理输入:



Because we’ve already specified the .unlimited number of values, we return .none so the max limit remains the same.

因为我们已经指定了.unlimited个值,所以我们返回.none因此最大限制保持不变。

Finally, we provide the method for handling completions:

最后,我们提供了处理完成的方法:



As we can see, we simply print the completion event.

如我们所见,我们仅打印完成事件。

Now let’s test the StringSubscriber:

现在让我们测试StringSubscriber



We supply an array of Strings to a publisher and then subscribe the StringSubscriber to that publisher.

我们向发布者提供String数组,然后将StringSubscriber订阅到该发布者。

As a result, we see the output we wanted:

结果,我们看到了想要的输出:




(Making a Subscriber Work With Any Type)

You may think, working only with String objects is good, but how do I make a subscriber work with any type?

您可能会认为,仅使用String对象是好的,但是如何使订阅者使用任何类型的对象呢?

The solution is to make it generic, as follows:

解决方案是使其通用,如下所示:


To test it, create two publishers and subscribe to them:

要对其进行测试,请创建两个发布者并订阅它们:


We can see this prints out values, as expected:

我们可以看到,此打印出了预期的值:


(Resources)

The source code is available in a GitHub gist:

源代码可在GitHub上找到:


(Wrapping Up)

Interested in other Swift features? Feel free to check out my other relevant articles:

对其他Swift功能感兴趣吗? 随时查看我的其他相关文章:

  • “What Is the Difference Between Class and Static in Swift?” “在Swift中,类和静态之间有什么区别? ”
  • “What Is the ~= Operator in Swift?” “在Swift中,〜=运算符是什么? ”
  • “What Is the vDSP Framework in Swift?” “ Swift中的vDSP框架是什么? ”
  • “What Is the Value-Binding Pattern in Swift?” “ Swift中的价值绑定模式是什么? ”
  • “What Is the CustomStringConvertible Protocol in Swift?” “ Swift中的CustomStringConvertible协议是什么? ”

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/how-to-create-your-own-combine-subscriber-in-swift-5-702b3f9c68c4

swift 订阅


https://www.xamrdz.com/mobile/4gk1921867.html

相关文章: