Collections

Store and organize data using arrays, dictionaries, sets, and other data structures.

Topics

Arrays and Dictionaries

struct Array

An ordered, random-access collection.

struct Dictionary

A collection whose elements are key-value pairs.

Sets

struct Set

An unordered collection of unique elements.

protocol OptionSet

A type that presents a mathematical set interface to a bit set.

Ranges

Create a collection of all the values in a range by using the half-open (..<) and closed (...) range operators.

struct CountableRange

A half-open range that forms a collection of consecutive values.

struct CountableClosedRange

A closed range that forms a collection of consecutive values.

Strides

Create a stride that steps over values between two boundaries using the stride(from:to:by:) and stride(from:through:by:) functions.

func stride<T>(from: T, to: T, by: T.Stride)

Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.

func stride<T>(from: T, through: T, by: T.Stride)

Returns a sequence from a starting value toward, and possibly including, an end value, stepping by the specified amount.

Special-Use Collections

These collections can store zero, one, or many of the same element.

func repeatElement<T>(T, count: Int)

Creates a collection containing the specified number of the given element.

struct CollectionOfOne

A collection containing a single element of type Element.

struct EmptyCollection

A collection whose element type is Element but that is always empty.

struct DictionaryLiteral

A lightweight collection of key-value pairs.

Dynamic Sequences

func sequence<T>(first: T, next: (T) -> T?)

Returns a sequence formed from first and repeated lazy applications of next.

func sequence<T, State>(state: State, next: (inout State) -> T?)

Returns a sequence formed from repeated lazy applications of next to a mutable state.

Joint Iteration

func zip<Sequence1, Sequence2>(Sequence1, Sequence2)

Creates a sequence of pairs built out of two underlying sequences.

Advanced Collection Topics

Sequence and Collection Protocols

Write generic code that works with any collection, or build your own collection types.

Supporting Types

Use wrappers, indices, and iterators in operations like slicing, flattening, and reversing a collection.

Managed Buffers

Build your own buffer-backed collection types.

See Also

Values and Collections

Numbers and Basic Values

Model data with numbers, Boolean values, and other fundamental types.

Strings and Text

Work with text using Unicode-safe strings.