gpt4 book ai didi

ios - ReactiveCocoa - 发出数组中最新 N 个值的 SignalProducer

转载 作者:行者123 更新时间:2023-11-28 08:48:27 24 4
gpt4 key购买 nike

我有一个 SignalProducer ProducerA,它以不同的时间间隔发出值。我正在尝试收集 SignalProducer 发出的最新 N 个值并创建一个新的生产者 ProducerB,它发出一个包含最新 N 个值的数组。

ProducerB 应在 ProducerA 发出前 N 个值时开始发出值,然后在每次 ProducerA 发出新值时发出一个新数组。

有人可以帮助我吗?

最佳答案

我想出了这个代码

extension SignalProducer {
/// Creates a new producer that emits an array that contains the latest N values that were emitted
/// by the original producer as specified in 'capacity'.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
public func latestValues(n:Int) -> SignalProducer<[Value], Error> {
var array: [Value] = []
return self.map {
value in

array.append(value)

if array.count >= n {
array.removeFirst(array.count - n)
}

return array
}
.filter {
$0.count == n
}
}
}

关于ios - ReactiveCocoa - 发出数组中最新 N 个值的 SignalProducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664542/

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