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 in
switch 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:
上面的代码在控制台中的输出为:
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 in
return 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 in
print(value)
})
The above code prints [1]
since it was the last emitted item.
上面的代码打印[1]
因为它是最后发出的项目。
And that brings an end to this tutorial.
这样就结束了本教程。
rxswift