实例方法
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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。