Book Image

Swift 2 Design Patterns

By : Julien Lange
Book Image

Swift 2 Design Patterns

By: Julien Lange

Overview of this book

Table of Contents (15 chapters)
Swift 2 Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The iterator pattern


This pattern is commonly used in many languages with an array or a collection of objects. It allows iteration over a list of objects contained in a collection.

Roles

The iterator pattern allows you to iterate sequentially over an aggregated object of objects without having to know how the collection is structured.

Design

Here, you'll find the generic UML class diagram of the pattern, but note that we will not implement it using this way.

Indeed, Swift provides some types that will simplify the implementation of the iterator pattern, without having to produce all of the needed requirements by hand.

Why reinvent the wheel? The following figure represents the generic UML class diagram:

Participants

As with every pattern described up to now, I will tell you what the participants in this pattern are even if we will not see all of them in our implementation of the pattern:

  • Collection: This is an abstract class that implements the association of the collection with items and the CreateIterator...