What is the difference between SequenceType and CollectionType in swift? -
please explain difference between sequencetype
, generatortype
, collectiontype
in swift programming language.
also, if implementing own data structure advantage of using sequencetype
, generatortype
or collectiontype
protocols?
generatortype (iteratorprotocol in swift 3): generators
can give next
element of sequence, if there no element returns nil
. generators
encapsulates iteration state , interfaces iteration on sequence.
a generator works providing single method, namely – next()
, returns next value underlying sequence
.
following classes adopt generatortype protocol:
dictionarygenerator, emptygenerator, more here.
sequencetype (sequence in swift 3): sequence
represent series of values. sequence
type can iterated for...in
loop.
essentially sequence generator factory; knows how make generators sequence.
following classes adopt sequencetype protocol:
nsarray, nsdictionary, nsset , more.
collectiontype (collection in swift 3): collection
sequencetype
can accessed via subscript , defines startindex
, endindex
. collection
step beyond sequence; individual elements of collection can accessed multiple times.
collectiontype
inherits sequencetype
following classes adopt collectiontype protocol:
array, dictionary, set, range , more.
Comments
Post a Comment