rxswift

In this tutorial, we’ll be discussing the basics of RxSwift in our XCode Project. Before we get into the nitty-gritty details, lets setup RxSwift.

在本教程中,我们将在XCode项目中讨论RxSwift的基础知识。 在深入了解细节之前,让我们设置RxSwift。

设置RxSwift (Setting Up RxSwift)

RxSwift is an iOS Library. Before we discuss what’s more in store lets create a new XCode Project.
Goto the folder path from your terminal and do a pod init.

RxSwift是一个iOS库。 在讨论商店中的更多内容之前,让我们创建一个新的XCode Project。
从终端转到文件夹路径,然后执行pod init

A new PodFile gets created in your XCode Project.
Open the PodFile using vim Podfile and add the following statement below use_frameworks!.

在您的XCode项目中创建了一个新的PodFile。
使用vim Podfile打开vim Podfile并在use_frameworks下面添加以下语句

pod 'RxSwift'
pod 'RxCocoa'

Now in your terminal, do a pod install.

现在在您的终端中,执行pod install

Congrats! You can now import RxSwift in your Project.
Now close your project and open it using the new xcworkspace file created.

恭喜! 现在,您可以在项目中导入RxSwift。
现在关闭您的项目,并使用创建的新xcworkspace文件将其打开。

RxSwift基础 (RxSwift Basics)

RxSwift is a reactive programming used for iOS Development. It follows the paradigm wherein it responds to changes.

RxSwift是用于iOS开发的React式编程。 它遵循对变化做出响应的范式。

RxSwift consists of two main components – Observable and Observer.

RxSwift由两个主要组件组成– Observable和Observer。

Observable emits items.
An observer which is Subscribed to the Observable watches those items.

可观察到的发射出物品。
订阅可观察对象的观察者观看这些项目。

Using Operators we can transform the items.

使用运算符,我们可以变换项目。

Now that we’ve successfully installed the RxSwift pod, lets import RxSwift in our ViewController.swift file.

既然我们已经成功安装了RxSwift容器,就可以在ViewController.swift文件中import RxSwift了。

可观察的 (Observables)

Observables in RxSwift can be defined in the following ways:

RxSwift中的Observable可以通过以下方式定义:

let justObservable = Observable.just("Hello RxSwift")
let arrayObservable = Observable.from([1,2,3])
let dictionaryObservable = Observable.from([1:"Hello",2:"Rx"])

We can subscribe to an observable sequences by calling
subscribe(on:(Event )-> ()) over it.

我们可以通过调用订阅一个可观察的序列
subscribe(on:(Event )-> ()) subscribe(on:(Event )-> ()) 超过它。

订阅 (Subscribing)

let dictSubscribe = dictionaryObservable.subscribe{event in print(event)}let arraySubscription = arrayObservable.subscribe { event inswitch event {case .next(let value):print(value)case .error(let error):print(error)case .completed:print("completed")}}

The events emitted in the subscribe method are sequences.
An event is an enum type which can have the following three possible values:

subscription方法中发出的事件是序列。
事件是枚举类型,可以具有以下三个可能的值:

  • .next(value: T) — Each value emitted is returned in the value here..next(value:T) -发出的每个值都在此处的值中返回。
  • .error(error: Error) — This gets called when there is an error..error(错误:错误)发生错误时调用此方法。
  • .completed — This gets called after all the items are emitted.completed —在发出所有项目后调用

The output of the above code in the console is:

上面的代码在控制台中的输出为:

RxSwift simplifies Multi threading.
RxSwift简化了多线程。

To cancel a subscription we can call dispose on the subscription instance.
Another way to dispose subscriptions is to add them to a DisposeBag.

要取消订阅,我们可以在订阅实例上调用dispose
处置订阅的另一种方法是将它们添加到DisposeBag中

let bag = DisposeBag()
bag.addDisposableTo(dictSubscribe)

When deinit for the DisposableBag instance is called, the subscription is automatically canceled.

调用DisposableBag实例的deinit时 ,订阅将自动取消。

操作员 (Operator)

We can transform the emitted items in RxSwift using Operators.

我们可以使用运算符在RxSwift中转换发射的项目。

Operators that are commonly used are:

常用的运算符有:

  • map地图
  • filter过滤

Using map operator:

使用地图运算符:

Observable<Int>.of(1,2,3,4,5,6,7).map { value inreturn value * value}.subscribe(onNext:{print($0)})

Output is :

输出为:

The filter operator is used to filter the emitted items such that items that pass the condition would be shown in the final emission.

过滤器运算符用于过滤发射的项目,以便通过条件的项目将显示在最终发射中。

Observable<Int>.of(1,2,3,4,5,6,7).filter{$0>8}.subscribe(onNext:{print($0)})

FlatMap

平面图

FlatMap is used to create a new sequence from other sequences.

FlatMap用于从其他序列创建新序列。

let observable1  = Observable<Int>.of(1,2)
let observable2  = Observable<Int>.of(3,4)let observableOfObservables = Observable.of(observable1,observable2)observableOfObservables.subscribe(onNext:{print($0)})observableOfObservables.flatMap{ return $0 }.subscribe(onNext:{print($0)})

The output is:

输出为:

As you can see a FlatMap flattens the arrays.

如您所见,FlatMap展平了数组。

科目 (Subjects)

Subjects are Observables only, but with special properties.

主题仅是可观察对象,但具有特殊属性。

Subjects can be divided into the following types:

主题可以分为以下几种类型:

  • PublishSubject: This emits all items when subscribed.PublishSubject :订阅后将发出所有项目。
  • BehaviourSubject: The subscriber when subscribed to such observables, would get only the last recently emitted item.BehaviourSubject :订阅者订阅此类可观察项时,将仅获得最近发出的最新项。
  • ReplaySubject: All the items would be replayed. You can specify how items you want to get replayed.ReplaySubject :将重播所有项目。 您可以指定如何重播项目。

Let’s look at an example of BehaviourSubject:

让我们看一个BehaviourSubject的例子:

let subject = BehaviorSubject(value: [10, 20])subject.onNext([1])subject.asObserver().subscribe(onNext: { value inprint(value)})

The above code prints [1] since it was the last emitted item.

上面的代码打印[1]因为它是最后发出的项目。

And that brings an end to this tutorial.

这样就结束了本教程。

翻译自: https://www.journaldev.com/22691/rxswift

rxswift

rxswift_RxSwift相关推荐

最新文章

  1. The note of Developing Innovative Ideas for New Companies Course
  2. 使用 Eclipse Memory Analyzer 进行堆转储文件分析
  3. 一个老鸟发的公司内部整理的 Android 学习路线图 Markdown 版本
  4. Java的时间为何从1970年1月1日开始
  5. java筑基期(6)----javascript(高级(2))
  6. JSP简单练习-上传文件
  7. 今日分享:vue3多层嵌套组件如何访问到最外层组件的数据?
  8. 是时候开始用C#快速开发移动应用了
  9. android文件添加一行代码怎么写,Android:以编程方式添加Textview,而不是将文本包装到下一行(示例代码)...
  10. CS和BS结构的优缺点
  11. 公开封尘已久的即时通讯源码(转)
  12. ensp下载与安装问题
  13. MT6739充电IC集成步骤
  14. 新浪开放平台:解决获取access_token抛 21323 异常,以及接口调用
  15. Linux运维大牛带你认识真正 Linux 系统结构!超强入门技术文!
  16. 设计一款理财产品并发布推广
  17. CSS-Sprite(雪碧图)
  18. 阿里云网站注销备案的办法
  19. zip直链生成网站_手把手教你如何用飞桨自动生成二次元人物头像
  20. 《变形金刚4》将如期上映 植入广告无孔不入

热门文章

  1. Napster:生死难料,前途未卜
  2. jupyter notebook多行注释方法
  3. SQLServer uniqueidentifier 类型
  4. python pymysql cursors_怎么Pythonpymysql.cursors从mysql存储过程获取INOUT返回结果
  5. 普通物理拾遗----热学
  6. 方舟服务器怎么请求芯片,方舟指令哔哩芯片这么获得?哔哩芯片获得方法详解分享...
  7. GWT(Google Web Toolkit)初体验
  8. 从0了解矩阵——矩阵的本质
  9. CentOS安装XenServer Tools
  10. 关于mysql中5位数字转化为日期格式的问题