Sometimes you have a struct/class that holds some kind of sequence and
you would like to iterate over it with for ... in .... In
Swift the struct/class has to conform to SequenceType to
enable this iteration.
Let’s say you have a simple struct to hold domains:
To conform to SequenceType the
struct needs to implement the method:
and the Generator is of type
GeneratorType. Ok, let’s start with the generator.
The protocol GeneratorType looks
like this:
So, the generator need to implement a method next() that returns
the next element until no element is left.
A simple generator for the Domains struct could look like this:
The method is mutating because it changes the index property.
The Domains struct would then conform to SequenceType like
this: