Ios 如何从rx观测值中提取最后十个元素?
我有一个Ios 如何从rx观测值中提取最后十个元素?,ios,swift,zip,system.reactive,take,Ios,Swift,Zip,System.reactive,Take,我有一个可观察的,它将另外三个可观察的组合起来,然后发出一个数组。从这个合并的数组中我想取最后10个对象。但是我想我用最后十个数组来代替。首先尝试使用compactMap,但没有奏效。它仍然作为数组返回 return Observable.combineLatest(breakfast, dessert, cookies) { $0 + $1 + $2 } .compactMap { $0 }.takeLast(10) .do(onNext: { [weak self] valu
可观察的
,它将另外三个可观察的
组合起来,然后发出一个数组
。从这个合并的数组中
我想取最后10个对象。但是我想我用最后十个数组来代替。首先尝试使用compactMap
,但没有奏效。它仍然作为数组返回
return Observable.combineLatest(breakfast, dessert, cookies) { $0 + $1 + $2 }
.compactMap { $0 }.takeLast(10)
.do(onNext: { [weak self] value in
self?.content.accept(value.compactMap {
NewRecipesCollectionViewCellViewModel(recipe: $0)})
})
通过这种方法,您可以一个接一个地返回元素:
let breakfast = Observable.just(["egg", "milk"])
let dessert = Observable.just(["ice cream", "chocolate"])
let cookies = Observable.just(["cookie1", "cookie2"])
Observable.combineLatest(breakfast, dessert, cookies)
.map { $0 + $1 + $2 }
.do(onNext: { foods in
let lastTenFood = foods.suffix(10) // returns the last 10 elements from the array
for food in lastTenFood {
print("Food: \(food)")
}
})
.subscribe()
.disposed(by: disposeBag)
谢谢,但它说observable没有一个叫做它的操作符。