protocol
GeneratorType
Inheritance | View Protocol Hierarchy → |
---|---|
Associated Types |
The type of element generated by |
Import |
|
Instance Methods
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 self.next()
has returned nil
. Specific implementations of this protocol
are encouraged to respond to violations of this requirement by
calling preconditionFailure("...")
.
Declaration
mutating
func
next
() -
>
Self
.
Element
?
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
for
...in
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 toCollectionType
. Also, the generators must be obtained by distinct calls to the sequence'sgenerate()
method, rather than by copying.