4

似乎 Combine(随 Xcode 11 beta 7 提供)缺少distinct操作员?

谁能帮我建一个?:)

4

2 回答 2

9

啊,我多么愚蠢,有一个,它被称为RemoveDuplicates,并链接到方法removeDuplicates()

于 2019-09-02T17:26:10.710 回答
5

.removeDuplicates()等价于.distinctUntilChanged()

您可以执行以下操作来获取.distinct()

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Self.Output : Equatable {
    public func distinct() -> AnyPublisher<Self.Output, Self.Failure> {
        self.scan(([], nil)) {
            $0.0.contains($1) ? ($0.0, nil) : ($0.0 + [$1], $1)
        }
        .compactMap { $0.1 }
        .eraseToAnyPublisher()
    }
}
于 2019-09-25T02:21:36.097 回答