Operator之数据匹配(Applying matching criteria to elements)

allSatisfy/tryAllSatisfy

当publisher输出的所有数据都让闭包返回true,pipline才会输出true。

cancellables = Set<AnyCancellable>()  
let publisher = PassthroughSubject<String, Never>()

  publisher
      .allSatisfy { value in
          value.count > 2
      }
      .sink { print($0) }
      .store(in: &cancellables)

  publisher.send("abc")
  publisher.send("defg")
  publisher.send("hijklmn")

contains

  • 判断publisher输出的数据是否符合条件

  • 一旦发现了符合条件的数据,立刻终止pipline并返回true

  • 如果publisher发送了.finished,未发现符合条件的数据,则返回false

cancellables = Set<AnyCancellable>()

[1, 2, 3]
    .publisher
    .contains(2)
    .sink { print($0) }
    .store(in: &cancellables)

containsWhere/tryContainsWhere

.contains{ someval -> Bool in
    return someval == "abc"
}

Made with in Shangrao,China By 老雷

Copyright © devler.cn 1987 - Present

赣ICP备19009883号-1