Ankit Goel Ankit Goel - 5 months ago 15
Swift Question

What is the difference between SequenceType and CollectionType in swift?

Please explain the difference between

SequenceType
,
GeneratorType
and
CollectionType
in the Swift programming language.

Also, if I am implementing my own data structure what would be the advantage of using
SequenceType
,
GeneratorType
or
CollectionType
protocols?

Answer

GeneratorType: Generators is something that can give the next element of some sequence, if there is no element it returns nil. It encapsulates iteration state and interface for iteration over a sequence.

A generator works by providing a single method, namely, next(), which simply returns the next value from the underlying sequence. next()


SequenceType: A Sequence represent a series of values. It is a type that can be iterated with a for...in loop.

Essentially a sequence is a generator factory; something that knows how to make generators for a sequence.


CollectionType: Collection is a SequenceType that can be accessed via subscript and defines a startIndex and endIndex. Collection is a step beyond a sequence in that individual elements of a collection can be accessed multiple times.


Form more information you can see this, this, and this