fileprivate lazy var bag: DisposeBag = DisposeBag()//按钮点击btn.rx.tap.subscribe { (event:Event<()>) in print("ddd") }.disposed(by: bag)//文字改变textField.rx.text.subscribe { (event:Event<(String?)>) in print(event) }.disposed(by: bag)//kvo myLabel.rx.observe(String.self, "text") .subscribe(onNext: { (str : String?) in print(str ?? "") }).disposed(by: bag) myLabel.text = "dacai"//监听tableView 滚动 scrool.contentSize = CGSize(width: 1000, height: 0) scrool.rx.contentOffset .subscribe(onNext: { (point : CGPoint) in print(point) }).disposed(by: bag)
基本操作
class ViewController: UIViewController { fileprivate lazy var bag : DisposeBag = DisposeBag() override func viewDidLoad() { super.viewDidLoad() // 1.创建一个never的Obserable let neverO = Observable//创建订阅者.never() neverO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) // 2.创建一个empty的Obserable let emptyO = Observable .empty() emptyO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 3.创建一个just的Obserable let justO = Observable.just("coderwhy") justO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 4.创建一个of的Obserable let ofO = Observable.of("a", "b", "c") ofO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 5.创建一个from的Obserable let array = [1, 2, 3, 4, 5] let fromO = Observable.from(array) fromO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 6.创建一个create的Obserable let createO = createObserable() createO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) let myJustO = myJustObserable(element: "coderwhy") myJustO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 7.创建一个Range的Obserable let rangeO = Observable.range(start: 1, count: 10) rangeO.subscribe { (event : Event ) in print(event) }.addDisposableTo(bag) print("--------------------") // 8.repeatElement let repeatO = Observable.repeatElement("hello world") repeatO .take(5) .subscribe { (event : Event ) in print(event) } .addDisposableTo(bag) } }extension ViewController { fileprivate func createObserable() -> Observable { return Observable.create({ (observer : AnyObserver ) -> Disposable in observer.onNext("coderwhy") observer.onNext("18") observer.onNext("1.88") observer.onCompleted() return Disposables.create() }) } fileprivate func myJustObserable(element : String) -> Observable { return Observable.create({ (observer : AnyObserver ) -> Disposable in observer.onNext(element) observer.onCompleted() return Disposables.create() }) }}
//订阅者只能接受, 订阅之后发出的事件 let publishSub = PublishSubject() publishSub.onNext("18") publishSub.subscribe { (event : Event ) in print(event) }.disposed(by: bag) publishSub.onNext("coderwhy") //订阅者可以接受订阅之前的事件&订阅之后的事件 let replaySub = ReplaySubject .createUnbounded() replaySub.onNext("a") replaySub.onNext("b") replaySub.onNext("c") replaySub.subscribe { (event : Event ) in print(event) }.disposed(by: bag) replaySub.onNext("d") print("-------------------------") // 3.BehaviorSubject, 订阅者可以接受,订阅之前的最后一个事件 let behaviorSub = BehaviorSubject(value: "a") behaviorSub.subscribe { (event : Event ) in print(event) }.disposed(by: bag) behaviorSub.onNext("e") behaviorSub.onNext("f") behaviorSub.onNext("g") print("-------------------------") // 4.Variable /* Variable 1> 相当于对BehaviorSubject进行装箱 2> 如果想将Variable当成Obserable, 让订阅者进行订阅时, 需要asObserable转成Obserable 3> 如果Variable打算发出事件, 直接修改对象的value即可 4> 当事件结束时,Variable会自动发出completed事件 */ print("============") let variable = Variable("a") //variable.value = "b" variable.asObservable().subscribe { (event : Event ) in print(event) }.disposed(by: bag) variable.value = "c" variable.value = "d"
数据操作:
let array2 = array.map({ $0 * $0 }) print(array2) Observable.of(1, 2, 3, 4) .map { (num : Int) -> Int in return num * num } .subscribe { (event : Event ) in print(event) } .disposed(by: bag) // 3.flatMap使用 let stu1 = Student(score: Variable(80)) // let stu2 = Student(score: Variable(100)) let studentVariable = Variable(stu1) studentVariable.asObservable() .flatMap { (stu : Student) -> Observablein return stu.score.asObservable() } .subscribe { (event : Event ) in print(event) } .disposed(by: bag) stu1.score.value = 1000
https://www.jianshu.com/p/431f645cb805