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

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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