博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RxSwift
阅读量:6114 次
发布时间:2019-06-21

本文共 5708 字,大约阅读时间需要 19 分钟。

hot3.png

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) -> Observable
in return stu.score.asObservable() } .subscribe { (event : Event
) in print(event) } .disposed(by: bag) stu1.score.value = 1000

https://www.jianshu.com/p/431f645cb805

转载于:https://my.oschina.net/zhaodacai/blog/1602872

你可能感兴趣的文章
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
直播视频流技术名词
查看>>
网易跟贴这么火,背后的某个力量不可忽视
查看>>
企业级java springboot b2bc商城系统开源源码二次开发-hystrix参数详解(八)
查看>>
java B2B2C 多租户电子商城系统- 整合企业架构的技术点
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>
数据库的这些性能优化,你做了吗?
查看>>
某大型网站迁移总结(完结)
查看>>
mysql的innodb中事务日志(redo log)ib_logfile
查看>>
部署SSL证书后,网页内容造成页面错误提示的处理办法
查看>>
MS SQLSERVER通用存储过程分页
查看>>
60.使用Azure AI 自定义视觉服务实现物品识别Demo
查看>>
Oracle 冷备份
查看>>
jq漂亮实用的select,select选中后,显示对应内容
查看>>
C 函数sscanf()的用法
查看>>
python模块之hashlib: md5和sha算法
查看>>
linux系统安装的引导镜像制作流程分享
查看>>
解决ros建***能登录不能访问内网远程桌面的问题
查看>>