Member-only story
The Iterator Design Pattern in Swift
Move over it!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.5, and Swift 5.2.4
Prerequisites:
- You will be expected to be aware of how to make a Single View Application in Swift.
- We use the defer keyword in this article
Terminology:
Design Pattern: a general, reusable solution to a commonly occurring problem
Iterator: In Swift, a protocol that allows you to loop through a sequence. That is, the protocol supplies values one at a time
IteratorProtocol: A protocol that provides the values of a sequence one at a time
Sequence: A Protocol that provides sequenced, iterative access to elements of the sequence
I’ve previously published a rather nice article covering the IteratorProtocol and their use in Linked Lists. If you’ve knowledge of Linked Lists I’d recommend you read that article now, if not let us press on with this article!
The Iterator Design Pattern in Swift
The iterator pattern solves the problem of how we might sequentially traverse a collection of objects.