gpt4 book ai didi

swift - 如何在忽略 RxSwift 中的值的同时观察 .next 事件的多个变量

转载 作者:可可西里 更新时间:2023-11-01 01:08:54 30 4
gpt4 key购买 nike

我有一个具有 Variable 属性 numbertext 的类,我想观察对这两个变量所做的任何更改。不需要这些属性的值。

在我的真实案例中,我有超过 10 个 Variables 需要观察,所以也许应该可以将它们传递给 .asObservable() 的数组。

let number: Variable<Int>

let text: Variable<String>

func doSomethingWhenAnyValueWasChanged() {
// some code
}

我该如何实现?

我尝试以多种方式使用 Observable.combineLatest() 来做到这一点,但对我来说没有成功。也许我错过了什么?

最佳答案

来自documentation看起来 Combine Latest 将是您的最佳选择。

CombineLatest

when an item is emitted by either of two Observables, combine the latest item emitted by each Observable via a specified function and emit items based on the results of this function.

let number: Variable<Int>

let text: Variable<String>

_ = Observable.combineLatest(number.asObservable(), text.asObservable()) { x, y in
doSomethingWhenAnyValueWasChanged()
}

关于swift - 如何在忽略 RxSwift 中的值的同时观察 .next 事件的多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50129748/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com