0
0
Swiftprogramming~10 mins

Sequence protocol for custom iteration in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sequence protocol for custom iteration
Define Sequence struct/class
Implement makeIterator() method
Create Iterator struct/class
Implement next() method
Use for-in loop to iterate
next() returns next element or nil
Iteration ends when next() returns nil
This flow shows how to create a custom sequence by defining a type that returns an iterator, which produces elements one by one until none are left.
Execution Sample
Swift
struct Countdown: Sequence {
  let start: Int
  func makeIterator() -> Iterator {
    Iterator(current: start)
  }
  struct Iterator: IteratorProtocol {
    var current: Int
    mutating func next() -> Int? {
      if current <= 0 { return nil }
      defer { current -= 1 }
      return current
    }
  }
}

for number in Countdown(start: 3) {
  print(number)
}
This code defines a Countdown sequence that counts down from a start number to 1, printing each number.
Execution Table
Stepcurrent (Iterator)Condition (current <= 0)ActionOutput
13falsereturn 3, current becomes 23
22falsereturn 2, current becomes 12
31falsereturn 1, current becomes 01
40truereturn nil (end iteration)nil
💡 Iteration stops when next() returns nil because current is 0
Variable Tracker
VariableStartAfter 1After 2After 3Final
current32100
Key Moments - 2 Insights
Why does the iteration stop when current reaches 0?
Because in the next() method, when current <= 0, it returns nil, signaling the end of iteration as shown in execution_table step 4.
Why do we use 'defer { current -= 1 }' instead of subtracting before returning?
Using defer ensures the current value is returned first, then decreased. This matches the output sequence seen in execution_table steps 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'current' after step 2?
A2
B0
C1
D3
💡 Hint
Check the 'current' column in execution_table row for step 2 and after 2.
At which step does the next() method return nil, ending the iteration?
AStep 1
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Output' column in execution_table where output is nil.
If we change 'start' to 5, how many times will next() return a number before nil?
A5 times
B3 times
C4 times
D6 times
💡 Hint
Refer to variable_tracker and execution_table pattern for how many numbers are returned from start down to 1.
Concept Snapshot
Sequence protocol requires a makeIterator() method.
IteratorProtocol requires a next() method returning next element or nil.
Use for-in loops to iterate custom sequences.
next() returning nil ends iteration.
Use defer to update state after returning current value.
Full Transcript
This example shows how to create a custom sequence in Swift by defining a struct that conforms to Sequence. The struct provides a makeIterator() method that returns an iterator. The iterator conforms to IteratorProtocol and implements next(), which returns the next element or nil when done. The countdown example starts from a number and counts down to 1. Each call to next() returns the current number and then decreases it. When current reaches zero, next() returns nil, ending the iteration. The for-in loop uses this to print each number. The execution table traces each call to next(), showing current value, condition check, action, and output. The variable tracker shows how current changes after each step. Key moments clarify why iteration stops and why defer is used. The quiz tests understanding of variable changes and iteration end. This teaches how to build and use custom sequences in Swift simply and clearly.