19.响应式编程

2020-05-22  本文已影响0人  迷心迷

响应式编程

RxSwift

1 Podfile
use_frameworks!
target 'target_name' do
pod 'RxSwift', '~> 5'
pod 'RxCocoa', '~> 5'
end

2 命令行
pod repo update
ppod install

3 导入模块
import RxSwift
import RxCocoa

let observable = Observable<Int>.timer(.seconds(2), period: .seconds(1), scheduler: MainScheduler.instance)
        
        let _ = observable
            .takeUntil(self.rx.deallocated)
            .map { "数值是:\($0)" }
            .bind(to: label.rx.text)
        
        observable.subscribe(onNext: { element in
            print("next", element)
        }, onError: { error in
            print("error", error)
        }, onCompleted: {
            print("completed")
        }).disposed(by: bag)
        
        observable.subscribe { event in
            switch event {
            case .next(let element):
                print("next", element)
            case .error(let error):
                print("error", error)
            case .completed:
                print("completed")
            }
        }.disposed(by: bag)
上一篇 下一篇

猜你喜欢

热点阅读