RxSwift笔记九RxExtensions

简介

    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

转载于:https://juejin.im/post/5ab1fde65188255579188966

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值