简介
git地址: https://github.com/ReactiveX/RxSwift
参考资料:http://t.swift.gg/d/2-rxswift
ReactiveX是通过可观察的流实现异步编程的一种API,它结合了观察者模式、迭代器模式和函数式编程的精华,RxSwift 是 ReactiveX 编程思想的一种实现。
复制代码
RxOptional
git地址: https://github.com/RxSwiftCommunity/RxOptional
- filterNil
Observable<String?>
.of("One", nil, "Three")
.filterNil()
// Type is now Observable<String>
.subscribe { print($0) }
输出:
next(One)
next(Three)
completed
复制代码
- replaceNilWith
Observable<String?>
.of("One", nil, "Three")
.replaceNilWith("Two")
// Type is now Observable<String>
.subscribe { print($0) }
输出:
next(One)
next(Two)
next(Three)
completed
复制代码
- errorOnNil:(对Driver无效)
Observable<String?>
.of("One", nil, "Three")
.errorOnNil()
// Type is now Observable<String>
.subscribe { print($0) }
输出:
next(One)
error(Found nil while trying to unwrap type <Optional<String>>)
复制代码
- catchOnNil:
Observable<String?>
.of("One", nil, "Three")
.catchOnNil {
return Observable<String>.just("A String from a new Observable")
}
// Type is now Observable<String>
.subscribe { print($0) }
输出:
next(One)
next(A String from a new Observable)
next(Three)
completed
复制代码
- distinctUntilChanged
Observable<Int?>
.of(5, 6, 6, nil, nil, 3)
.distinctUntilChanged()
.subscribe { print($0) }
输出:
next(Optional(5))
next(Optional(6))
next(nil)
next(Optional(3))
completed
复制代码
NSObject-Rx
git地址: https://github.com/RxSwiftCommunity/NSObject-Rx
不用总是很麻烦的添加 let disposeBag = DisposeBag()
;直接调用rx.disposeBag
RxAlamofire
Alamofire的Rx扩展 git地址: https://github.com/RxSwiftCommunity/RxAlamofire
let formatter = NSNumberFormatter()
formatter.numberStyle = .CurrencyStyle
formatter.currencyCode = "USD"
if let fromValue = NSNumberFormatter().numberFromString(self.fromTextField.text!) {
RxAlamofire.requestJSON(.get, sourceStringURL)
.debug()
.subscribe(onNext: { [weak self] (r, json) in
if let dict = json as? [String: AnyObject] {
let valDict = dict["rates"] as! Dictionary<String, AnyObject>
if let conversionRate = valDict["USD"] as? Float {
self?.toTextField.text = formatter
.string(from: NSNumber(value: conversionRate * fromValue))
}
}
}, onError: { [weak self] (error) in
self?.displayError(error as NSError)
})
.addDisposableTo(disposeBag)
} else {
self.toTextField.text = "Invalid Input!"
}
复制代码
RxDataSources
git地址: https://github.com/RxSwiftCommunity/RxDataSources
RxGesture
https://github.com/RxSwiftCommunity/RxGesture
对手势的rx扩展 详情见git