protocol GeneratorType

Encapsulates iteration state and interface for iteration over a sequence.

Note: While it is safe to copy a generator, advancing one copy may invalidate the others.

Any code that uses multiple generators (or loops) over a single sequence should have static knowledge that the specific sequence is multi-pass, either because its concrete type is known or because it is constrained to CollectionType. Also, the generators must be obtained by distinct calls to the sequence's generate() method, rather than by copying.

Inheritance View Protocol Hierarchy →
Associated Types

The type of element generated by self.

Import import Swift

Instance Methods

mutating func next() Required

Advance to the next element and return it, or nil if no next element exists.

Requires: next() has not been applied to a copy of self since the copy was made, and no preceding call to has returned nil. Specific implementations of this protocol are encouraged to respond to violations of this requirement by calling preconditionFailure("...").


mutating func next() -> Element?