Reactive Programming in iOS with Combine

Feb 4 2021 Swift 5.3, macOS 11.0, Xcode 12.2

Part 4: Timing, Scheduling and Sequencing Operators

28. Sequencing Operators

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 27. Scheduling Operators Next episode: 29. Challenge: Sequence Operators

This video was last updated on Feb 4 2021

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

On their surface, Publishers are really just sequences, and set of sequence operators in Combine let you work with the sequence as a whole instead of individual values. As we go through these operators, you’ll notice that many have the same names as functions from the Swift Standard Library - but be sure to note any differences between those functions and the operators in Combine when you use them in your code.

example(of: "min") {
  // 1
  let publisher = [1, -50, 246, 0].publisher
  // 2
  publisher
    .print("publisher")
    .min()
    .sink(receiveValue: { print("Lowest value is \($0)") })
    .store(in: &subscriptions)
}
example(of: "max") {
  // 1
  let publisher = ["A", "F", "Z", "E"].publisher
  // 2
  publisher
    .print("publisher")
    .max()
    .sink(receiveValue: { print("Highest value is \($0)") })
    .store(in: &subscriptions)
}
example(of: "count") {
  let publisher = ["A", "B", "C"].publisher
  publisher
    .print("publisher")
    .count()
    .sink(receiveValue: { print("I have \($0) items") })
    .store(in: &subscriptions)
}

example(of: "output(at:)") {

  let publisher = ["A", "B", "C"].publisher
  publisher
    .print("publisher")
    .output(at: 1)
    .sink(receiveValue: { print("Value at index 1 is \($0)") })
    .store(in: &subscriptions)
}
example(of: "output(in:)") {
  let publisher = ["A", "B", "C", "D", "E"].publisher
  publisher
    .output(in: 1...3)
    .sink(receiveCompletion: { print($0) },
          receiveValue: { print("Value in range: \($0)") })
    .store(in: &subscriptions)
}