實例方法
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 Result.Publisher receive(on:options:)用法及代碼示例
- Swift Result.Publisher replaceNil(with:)用法及代碼示例
- Swift Result.Publisher reduce(_:_:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- Swift Result.Publisher tryCompactMap(_:)用法及代碼示例
- Swift Result.Publisher print(_:to:)用法及代碼示例
- Swift Result.Publisher sink(receiveValue:)用法及代碼示例
- Swift Result.Publisher eraseToAnyPublisher()用法及代碼示例
- Swift Result.Publisher setFailureType(to:)用法及代碼示例
- Swift Result.Publisher first(where:)用法及代碼示例
- Swift Result.Publisher output(at:)用法及代碼示例
- Swift Result.Publisher tryCatch(_:)用法及代碼示例
- Swift Result.Publisher dropFirst(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代碼示例
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher catch(_:)用法及代碼示例
- Swift Result.Publisher drop(while:)用法及代碼示例
- Swift Result.Publisher prefix(_:)用法及代碼示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代碼示例
- Swift Result.Publisher merge(with:_:)用法及代碼示例
- Swift Result.Publisher map(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher removeDuplicates()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。