我有一个 SignalProducer ProducerA,它以不同的时间间隔发出值。我正在尝试收集 SignalProducer 发出的最新 N 值,并创建一个新的生产者 ProducerB,它发出包含最新 N 值的数组。
当 ProducerA 发出前 N 个值时,ProducerB 应该开始发出值,然后每次 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 - SignalProducer,发出数组中最新的 N 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664542/