如何阵列RxSwift
问题描述:
观察值
有类有一个布尔属性如何阵列RxSwift
class Coupon: NSObject {
var invalid: Bool = false
}
我创建了一个数组[优惠],我需要one.when所有修改coupon.invalid为true一个这些coupon.invalid == true,然后我可以处理下一个任务,我如何使用RxSwift实现这个逻辑?请帮助我~~
答
首先,您将Coupon类型设置为结构而不是类。
struct Coupon {
var invalid = false
}
并将您的数组嵌入到变量中。
let coupons = Variable<[Coupon]>([])
coupons.value = getAllTheCoupons()
现在您可以创建一个observable,只要所有值都为true就会发出一个值。
let allTrue = coupons.asObservable()
.map { coupons in
coupons.contains(where: { $0.invalid == false }) == false
}
.filter { $0 }
allTrue.subscribe(onNext: { _ in
print("process next task.
}).disposed(by: bag)