Observe array in Swift 3 using RxSwift

前端 未结 2 1472
情话喂你
情话喂你 2021-02-08 12:09

To create an observable array using RxSwift in Swift 2, I use to do this:

[1, 2, 3].toObservable().subscribeNext { print($0) }

But

2条回答
  •  情话喂你
    2021-02-08 13:05

    In Swift 3 using RxSwift 3.0 I will do that like this:

    var array: Variable<[Int]> = Variable([1, 2, 3])
    array.asObservable().subscribe(onNext: {
            updatedArray in
            print(updatedArray)
    })
    array.value.append(4) // it will trigger `onNext` event 
    

    So the main difference is that you have to create an Variable object instead of using an explicit array.

提交回复
热议问题