Member-only story
Iterate through a Linked List in Swift
There’s an IteratorProtocol?
This article is about an implementation of Linked Lists in Swift, where you can iterate through the list. The theory of Linked Lists is covered in my own article, as are Linked Lists in Swift. This article is about doing better.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Coding in Swift Playgrounds (guide HERE)
This article uses Linked Lists as motivation for implementing the IteratorProtocol. It does help to have some understanding of Linked Lists, but this is not completely necessary.
My implementation makes use of the defer keyword in Swift.
Terminology
Swift terminology
Class: An object that defines properties and methods in common
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
Protocol: A blueprint on methods, properties and requirements to suit a piece of functionality