【问题标题】:Swift Combine: no `distinct` operator?Swift Combine:没有“distinct”运算符?
【发布时间】:2019-09-02 17:22:56
【问题描述】:

似乎Combine(Xcode 11 beta 7 附带)缺少distinct 运算符?

谁能帮我建一个? :)

【问题讨论】:

    标签: ios swift swiftui xcode11 combine


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      .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()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2016-06-01
        • 1970-01-01
        • 2014-02-21
        相关资源
        最近更新 更多