實例方法
remove
removeDuplicates()
僅發布與前一個元素不匹配的元素。
聲明
func removeDuplicates() -> Publishers.RemoveDuplicates<Self>
當
Output
符合 Equatable
時可用。返回值
使用(而不是發布)重複元素的發布者。
詳述
使用 Publisher/removeDuplicates()
從上遊發布者中刪除重複元素。該運算符具有兩個元素的內存:該運算符使用當前和以前發布的元素作為其比較的基礎。
在下麵的示例中,Publisher/removeDuplicates()
分別在 1
、3
和 4
的兩倍、三倍和四倍出現時觸發。因為二元內存隻考慮當前元素和前一個元素,所以運算符會在示例數據中打印最終的 0
,因為它的直接前身是 4
。
let numbers = [0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 0]
cancellable = numbers.publisher
.removeDuplicates()
.sink { print("\($0)", terminator: " ") }
// Prints: "0 1 2 3 4 0"
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Optional.Publisher receive(on:options:)用法及代碼示例
- Swift Optional.Publisher replaceNil(with:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift Optional.Publisher debounce(for:scheduler:options:)用法及代碼示例
- Swift Optional.Publisher breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)用法及代碼示例
- Swift Optional.Publisher mapError(_:)用法及代碼示例
- Swift Optional.Publisher catch(_:)用法及代碼示例
- Swift Optional.Publisher tryAllSatisfy(_:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher sink(receiveValue:)用法及代碼示例
- Swift Optional.Publisher scan(_:_:)用法及代碼示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代碼示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代碼示例
- Swift Optional.Publisher collect(_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher share()用法及代碼示例
- Swift Optional.Publisher merge(with:_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:)用法及代碼示例
- Swift Optional.Publisher assign(to:on:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher output(in:)用法及代碼示例
- Swift Optional.Publisher tryReduce(_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:_:)用法及代碼示例
- Swift Optional.Publisher prepend(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional.Publisher removeDuplicates()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。