使用RxSwift在Swift 3中观察数组 [英] Observe array in Swift 3 using RxSwift

查看:174
本文介绍了使用RxSwift在Swift 3中观察数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Swift 2中使用RxSwift创建一个可观察的数组,我可以这样做:

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

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

但是在Swift 3中,它不再起作用了,我得到了这个错误:

But in Swift 3, it doesn't work anymore, I got this error:

"[Int]"类型的值没有成员"toObservable"

Value of type '[Int]' has no member 'toObservable'

如何从swift数组创建RxSwift可观察数组?

How can I create an RxSwift observable array from a swift array?

推荐答案

在Swift 3中使用RxSwift 3.0我将像这样:

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 

所以主要区别在于您必须创建一个Variable对象,而不是使用显式数组.

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

这篇关于使用RxSwift在Swift 3中观察数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆