Write generic code that works with any collection, or build your own collection types.
Sequence and Collection Protocols
Topics
First Steps
protocol Sequence
A type that provides sequential, iterated access to its elements.
protocol Collection
A sequence whose elements can be traversed multiple times, nondestructively, and accessed by an indexed subscript.
Collection Traversal
protocol Bidirectional Collection
A collection that supports backward as well as forward traversal.
protocol Random Access Collection
A collection that supports efficient random-access index traversal.
Collection Mutability
protocol Mutable Collection
A collection that supports subscript assignment.
protocol Range Replaceable Collection
A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.
Manual Iteration
protocol Iterator Protocol
A type that supplies the values of a sequence one at a time.
Algebraic Sets
protocol Set Algebra
A type that provides mathematical set operations.
Lazy Collections
protocol Lazy Sequence Protocol
A sequence on which normally-eager operations such as map
and filter
are implemented lazily.
protocol Lazy Collection Protocol
A collection on which normally-eager operations such as map
and filter
are implemented lazily.
See Also
Advanced Collection Topics
Use wrappers, indices, and iterators in operations like slicing, flattening, and reversing a collection.
Build your own buffer-backed collection types.