Photo by Foo Visuals on Unsplash

Iterate through the elements of a collection

Swift — Problems Catalogue #16

Alex Ilovan
Published in
3 min readJan 17, 2023

--

Problem Definition:

Consider the following scenario. You have to implement an app for a restaurant that allows users to browse the menu and place orders. The app would need to go through a collection of menu items, displaying information and images for each one as the user scrolls through the menu.

Problem Solution:

Solution —Iterator Pattern it’s a design pattern that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It allows the traversal of a collection of items in a uniform way, regardless of the implementation of the collection.

Real-World Usage:

1. Menu Iterator & Menu Protocols

First thing first, let’s start with the good ole’ protocol definitions.

The ‘MenuIterator’ protocol defines two methods that all iterator objects must implement:

‘hasNext() -> Bool’: This method returns ‘true’ if there are more items to iterate over, and ‘false’ otherwise.

‘next()-> MenuItem?’: This method returns the next menu item if there is one or ‘nil’ if there are no more items to iterate over.

The ‘Menu’ protocol defines one method that all objects that represent a menu must implement:

‘createIterator()->MenuIterator’: This method returns an object that implements the ‘MenuIterator’ protocol and can be used to iterate over the menu’s items.

2. MenuItem Struct

Second, let’s define the ‘MenuItem’ struct. This represents the menu items with various properties like name, description, isVegetarian, and price.

3. DinerMenuIterator & DinerMenu

Third, we need to create a ‘DinerMenuIterator’, which conforms to the ‘MenuIterator’ protocol that keeps track of the current position in an array of ‘MenuItem’ objects.

‘DinerMenu’ is an implementation of the ‘Menu’ protocol that holds an array of ‘MenuItem’ objects and can create an iterator for that array.

4. Bringing everything together

Last but not least, we create a DinerMenu instance and add some menu items to it using the ‘addItem’ method. Then, we use the ‘createIterator()’ method to create an iterator for the menu and assign it to a variable called ‘iterator’.

We use a ‘while’ loop to iterate over the menu items using the ‘hasNext()’ and ‘next()’ methods of the iterator. At each iteration of the loop, the ‘next()’ method returns the next menu item, and we print out the name, description and price. If there are no more items to iterate over, the ‘hasNext()’ method will return ‘false’ and the loop will exit.

From this point on, the sky is the limit 🚀 well…almost.

Of course, this design pattern has its limitations but used in moderation, it’s a great tool in our development toolbox.

This is the next article in the Swift Problems Catalogue series in which I’ll tackle general software development problems. The aim is to have a quick reference guide that can be easily accessed when having a design/algorithm dillemma.

Let me know what you think and don’t be shy to share where and when this pattern simplified your coding experience 🎶

--

--

Alex Ilovan
salt&pepper

🚀Head of Mobile Development @S&P 💻Comp. Engineer 🪐Engineering Manager. You can visit at: https://www.linkedin.com/in/alex-ilovan-129161b4/