What is the difference between SequenceType and CollectionType in swift?

前端 未结 1 716
悲哀的现实
悲哀的现实 2021-01-30 21:56

Please explain the difference between SequenceType, GeneratorType and CollectionType in the Swift programming language.

Also, if I

1条回答
  •  梦毁少年i
    2021-01-30 22:00

    GeneratorType (IteratorProtocol in Swift 3): Generators is something that can give the next element of some sequence, if there is no element it returns nil. Generators encapsulates iteration state and interfaces 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.

    Following classes Adopt GeneratorType Protocol:

    DictionaryGenerator, EmptyGenerator, more here.


    SequenceType (Sequence in Swift 3): A Sequence represent a series of values. Sequence 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.

    Following classes Adopt SequenceType Protocol:

    NSArray, NSDictionary, NSSet and more.


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

    CollectionType inherits from SequenceType

    Following classes Adopt CollectionType Protocol:

    Array, Dictionary, Set, Range and more.


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

    0 讨论(0)
提交回复
热议问题