Store and organize data using arrays, dictionaries, sets, and other data structures.
Collections
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 Option Set
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.
static func ..< (Self, Self) -> Range<Self>
Returns a half-open range that contains its lower bound but not its upper bound.
struct Range
A half-open interval from a lower bound up to, but not including, an upper bound.
static func ... (Self, Self) -> Closed Range<Self>
Returns a closed range that contains both of its bounds.
struct Closed Range
An interval from a lower bound up to, and including, an upper bound.
Strides
Create a stride that steps over values between two boundaries using the stride(from:
and stride(from:
functions.
func stride<T>(from: T, to: T, by: T .Stride) -> Stride To<T>
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) -> Stride Through<T>
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 repeat Element<T>(T, count: Int) -> Repeated<T>
Creates a collection containing the specified number of the given element.
struct Collection Of One
A collection containing a single element.
struct Empty Collection
A collection whose element type is Element
but that is always empty.
struct Key Value Pairs
A lightweight collection of key-value pairs.
typealias Dictionary Literal
DeprecatedDynamic Sequences
func sequence<T>(first: T, next: (T) -> T?) -> Unfold First Sequence<T>
Returns a sequence formed from first
and repeated lazy applications of next
.
func sequence<T, State>(state: State, next: (inout State) -> T?) -> Unfold Sequence<T, State>
Returns a sequence formed from repeated lazy applications of next
to a mutable state
.
Joint Iteration
func zip<Sequence1, Sequence2>(Sequence1, Sequence2) -> Zip2Sequence<Sequence1, Sequence2>
Creates a sequence of pairs built out of two underlying sequences.
Advanced Collection Topics
Write generic code that works with any collection, or build your own collection types.
Use wrappers, indices, and iterators in operations like slicing, flattening, and reversing a collection.
Build your own buffer-backed collection types.
See Also
Values and Collections
Model data with numbers, Boolean values, and other fundamental types.
Work with text using Unicode-safe strings.