Spring SALE

Iterator in Swift

Iterator is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details.

Thanks to the Iterator, clients can go over elements of different collections in a similar fashion using a single iterator interface.



Usage examples: The pattern is very common in Swift code. Many frameworks and libraries use it to provide a standard way for traversing their collections.

Identification: Iterator is easy to recognize by the navigation methods (such as next, previous and others). Client code that uses iterators might not have direct access to the collection being traversed.

The following examples are available on Swift Playgrounds.
Kudos to Alejandro Mohamad for creating the Playground version.

Iterator in Other Languages

Iterator in C# Iterator in C++ Iterator in Go Iterator in Java Iterator in PHP Iterator in Python Iterator in Ruby Iterator in Rust Iterator in TypeScript