Framework

Swift Standard Library

Solve complex problems, and write high-performance, readable code. Access the fundamental protocols, data types, and algorithms common to all Swift programs.

Overview

The Swift standard library defines a base layer of functionality for writing Swift programs, including:

Many language features have a corresponding protocol—any type that conforms to the protocol can be used with that feature. For example, a for-in loop can iterate over the elements of any value whose type conforms to the Sequence protocol, and any type that conforms to the ExpressibleByStringLiteral protocol can have values assigned from string literals.

By default, the standard library guarantees memory safety. Many functions and methods document the requirements that must be satisfied by the caller, such as an array index being valid; memory safety is guaranteed even if a requirement is violated. However, violating a requirement can trigger a runtime error. APIs that include the word “unsafe” in their name let you explicitly disable safety checks in places where you need the additional performance. It’s your responsibility to verify the memory safety of code that uses unsafe APIs. Memory safety is also not guaranteed if there is a race condition in multithreaded code.

Many functions and methods have documented performance characteristics. These characteristics are guaranteed only when all participating protocols' performance expectations are satisfied. If a participating type fails to satisfy these expectations, the functions and methods work as documented, except for their performance guarantees. Types provided by the standard library meet all performance expectations of the protocols they adopt, except where explicitly noted.

Symbols

Classes

ManagedBuffer

A class whose instances contain a property of type Header and raw storage for an array of Element, whose size is determined at instance creation.

Protocols

_Incrementable

This protocol is an implementation detail of Integer; do not use it directly.

AbsoluteValuable

A type that supports an “absolute value” function.

AnyObject

The protocol to which all classes implicitly conform.

BidirectionalCollection

A collection that supports backward as well as forward traversal.

BidirectionalIndexable

A type that provides subscript access to its elements, with bidirectional index traversal.

BinaryFloatingPoint

A radix-2 (binary) floating-point type.

BitwiseOperations

A type that supports standard bitwise arithmetic operators.

Collection

A sequence whose elements can be traversed multiple times, nondestructively, and accessed by indexed subscript.

Comparable

A type that can be compared using the relational operators <, <=, >=, and >.

CustomDebugStringConvertible

A type with a customized textual representation suitable for debugging purposes.

CustomLeafReflectable

A type that explicitly supplies its own mirror, but whose descendant classes are not represented in the mirror unless they also override customMirror.

CustomPlaygroundQuickLookable

A type that explicitly supplies its own playground Quick Look.

CustomReflectable

A type that explicitly supplies its own mirror.

CustomStringConvertible

A type with a customized textual representation.

CVarArg

Instances of conforming types can be encoded, and appropriately passed, as elements of a C va_list.

Equatable

A type that can be compared for value equality.

Error

A type representing an error value that can be thrown.

ExpressibleByArrayLiteral

A type that can be initialized using an array literal.

ExpressibleByBooleanLiteral

A type that can be initialized with the Boolean literals true and false.

ExpressibleByDictionaryLiteral

A type that can be initialized using a dictionary literal.

ExpressibleByExtendedGraphemeClusterLiteral

A type that can be initialized with a string literal containing a single extended grapheme cluster.

ExpressibleByFloatLiteral

A type that can be initialized with a floating-point literal.

ExpressibleByIntegerLiteral

Conforming types can be initialized with integer literals.

ExpressibleByNilLiteral

A type that can be initialized using the nil literal, nil.

ExpressibleByStringInterpolation

A type that can be initialized by string interpolation with a string literal that includes expressions.

ExpressibleByStringLiteral

A type that can be initialized with a string literal.

ExpressibleByUnicodeScalarLiteral

A type that can be initialized with a string literal containing a single Unicode scalar value.

FloatingPoint

A floating-point numeric type.

Hashable

A type that provides an integer hash value.

Indexable

A type that provides subscript access to its elements, with forward index traversal.

IndexableBase

A type that provides subscript access to its elements, with forward index traversal.

Integer

A set of common requirements for Swift’s integer types.

IntegerArithmetic

The common requirements for types that support integer arithmetic.

IteratorProtocol

A type that supplies the values of a sequence one at a time.

LazyCollectionProtocol

A collection on which normally-eager operations such as map and filter are implemented lazily.

LazySequenceProtocol

A sequence on which normally-eager operations such as map and filter are implemented lazily.

LosslessStringConvertible

A type that can be represented as a string in a lossless, unambiguous way.

MirrorPath

A protocol for legitimate arguments to Mirror’s descendant method.

MutableCollection

A collection that supports subscript assignment.

MutableIndexable

A type that provides subscript access to its elements.

OptionSet

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

RandomAccessCollection

A collection that supports efficient random-access index traversal.

RandomAccessIndexable

A collection that supports efficient random-access index traversal.

RangeReplaceableCollection

A collection that supports replacement of an arbitrary subrange of elements with the elements of another collection.

RangeReplaceableIndexable

A type that supports replacement of an arbitrary subrange of elements with the elements of another collection.

RawRepresentable

A type that can be converted to and from an associated raw value.

Sequence

A type that provides sequential, iterated access to its elements.

SetAlgebra

A type that provides mathematical set operations.

SignedInteger

A set of common requirements for Swift’s signed integer types.

SignedNumber

Instances of conforming types can be subtracted, arithmetically negated, and initialized from 0.

Strideable

Conforming types are notionally continuous, one-dimensional values that can be offset and measured.

TextOutputStream

A type that can be the target of text-streaming operations.

TextOutputStreamable

A source of text-streaming operations.

UnicodeCodec

A Unicode encoding form that translates between Unicode scalar values and form-specific code units.

UnsignedInteger

A set of common requirements for Swift’s unsigned integer types.

Structures

AnyBidirectionalCollection

A type-erased wrapper over any collection with indices that support bidirectional traversal.

AnyCollection

A type-erased wrapper over any collection with indices that support forward traversal.

AnyHashable

A type-erased hashable value.

AnyIndex

A wrapper over an underlying index that hides the specific underlying type.

AnyIterator

A type-erased iterator of Element.

AnyRandomAccessCollection

A type-erased wrapper over any collection with indices that support random access traversal.

AnySequence

A type-erased sequence.

Array

An ordered, random-access collection.

ArraySlice

A slice of an Array, ContiguousArray, or ArraySlice instance.

AutoreleasingUnsafeMutablePointer

A mutable pointer-to-ObjC-pointer argument.

BidirectionalSlice

A view into a subsequence of elements of another collection.

Bool

A value type whose instances are either true or false.

Character

A single extended grapheme cluster, which approximates a user-perceived character.

ClosedRange

An interval over a comparable type, from a lower bound up to, and including, an upper bound.

ClosedRangeIndex

A position in a CountableClosedRange instance.

ClosedRangeIterator

An iterator over the elements of a CountableClosedRange instance.

CollectionOfOne

A collection containing a single element of type Element.

ContiguousArray

A contiguously stored array.

CountableClosedRange

A closed range that forms a collection of consecutive values.

CountableRange

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

CVaListPointer

The corresponding Swift type to va_list in imported C APIs.

DefaultBidirectionalIndices

A collection of indices for an arbitrary bidirectional collection.

DefaultIndices

A collection of indices for an arbitrary collection.

DefaultRandomAccessIndices

A collection of indices for an arbitrary random-access collection.

Dictionary

A collection whose elements are key-value pairs.

DictionaryIndex

Used to access the key-value pairs in an instance of Dictionary<Key, Value>.

DictionaryIterator

An iterator over the members of a Dictionary<Key, Value>.

DictionaryLiteral

A lightweight collection of key-value pairs.

Double

A double-precision, floating-point value type.

EmptyCollection

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

EmptyIterator

An iterator that never produces an element.

EnumeratedIterator

The iterator for EnumeratedSequence.

EnumeratedSequence

An enumeration of the elements of a sequence or collection.

FlattenBidirectionalCollection

A flattened view of a base collection of collections.

FlattenBidirectionalCollectionIndex

A position in a FlattenBidirectionalCollection.

FlattenCollection

A flattened view of a base collection of collections.

FlattenCollectionIndex

A position in a FlattenCollection.

FlattenIterator

An iterator that produces the elements contained in each segment produced by some Base Iterator.

FlattenSequence

A sequence consisting of all the elements contained in each segment contained in some Base sequence.

Float

A single-precision, floating-point value type.

Float80

An extended-precision, floating-point value type.

IndexingIterator

A type that iterates over a collection using its indices.

Int

A signed integer value type.

Int16

A 16-bit signed integer value type.

Int32

A 32-bit signed integer value type.

Int64

A 64-bit signed integer value type.

Int8

An 8-bit signed integer value type.

IteratorOverOne

An iterator that produces one or fewer instances of Element.

IteratorSequence

A sequence built around an iterator of type Base.

JoinedIterator

An iterator that presents the elements of the sequences traversed by a base iterator, concatenated using a given separator.

JoinedSequence

A sequence that presents the elements of a base sequence of sequences concatenated using a given separator.

LazyBidirectionalCollection

A collection containing the same elements as a Base collection, but on which some operations such as map and filter are implemented lazily.

LazyCollection

A collection containing the same elements as a Base collection, but on which some operations such as map and filter are implemented lazily.

LazyFilterBidirectionalCollection

A lazy Collection wrapper that includes the elements of an underlying collection that satisfy a predicate.

LazyFilterCollection

A lazy Collection wrapper that includes the elements of an underlying collection that satisfy a predicate.

LazyFilterIndex

The Index used for subscripting a LazyFilterCollection.

LazyFilterIterator

An iterator over the elements traversed by some base iterator that also satisfy a given predicate.

LazyFilterSequence

A sequence whose elements consist of the elements of some base sequence that also satisfy a given predicate.

LazyMapBidirectionalCollection

A Collection whose elements consist of those in a Base Collection passed through a transform function returning Element. These elements are computed lazily, each time they’re read, by calling the transform function on a base element.

LazyMapCollection

A Collection whose elements consist of those in a Base Collection passed through a transform function returning Element. These elements are computed lazily, each time they’re read, by calling the transform function on a base element.

LazyMapIterator

The IteratorProtocol used by MapSequence and MapCollection. Produces each element by passing the output of the Base IteratorProtocol through a transform function returning Element.

LazyMapRandomAccessCollection

A Collection whose elements consist of those in a Base Collection passed through a transform function returning Element. These elements are computed lazily, each time they’re read, by calling the transform function on a base element.

LazyMapSequence

A Sequence whose elements consist of those in a Base Sequence passed through a transform function returning Element. These elements are computed lazily, each time they’re read, by calling the transform function on a base element.

LazyRandomAccessCollection

A collection containing the same elements as a Base collection, but on which some operations such as map and filter are implemented lazily.

LazySequence

A sequence containing the same elements as a Base sequence, but on which some operations such as map and filter are implemented lazily.

ManagedBufferPointer

Contains a buffer object, and provides access to an instance of Header and contiguous storage for an arbitrary number of Element instances stored in that buffer.

Mirror

Representation of the sub-structure and optional “display style” of any arbitrary subject instance.

MutableBidirectionalSlice

A view into a subsequence of elements of another collection.

MutableRandomAccessSlice

A view into a subsequence of elements of another collection.

MutableRangeReplaceableBidirectionalSlice

A view into a subsequence of elements of another collection.

MutableRangeReplaceableRandomAccessSlice

A view into a subsequence of elements of another collection.

MutableRangeReplaceableSlice

A view into a subsequence of elements of another collection.

MutableSlice

A view into a subsequence of elements of another collection.

ObjectIdentifier

A unique identifier for a class instance or metatype.

OpaquePointer

A wrapper around an opaque C pointer.

RandomAccessSlice

A view into a subsequence of elements of another collection.

Range

A half-open interval over a comparable type, from a lower bound up to, but not including, an upper bound.

RangeReplaceableBidirectionalSlice

A view into a subsequence of elements of another collection.

RangeReplaceableRandomAccessSlice

A view into a subsequence of elements of another collection.

RangeReplaceableSlice

A view into a subsequence of elements of another collection.

Repeated

A collection whose elements are all identical.

ReversedCollection

A collection that presents the elements of its base collection in reverse order.

ReversedIndex

An index that traverses the same positions as an underlying index, with inverted traversal direction.

ReversedRandomAccessCollection

A collection that presents the elements of its base collection in reverse order.

ReversedRandomAccessIndex

An index that traverses the same positions as an underlying index, with inverted traversal direction.

Set

An unordered collection of unique elements.

SetIndex

Used to access the members in an instance of Set<Element>.

SetIterator

An iterator over the members of a Set<Element>.

Slice

A view into a subsequence of elements of another collection.

StaticString

A string type designed to represent text that is known at compile time.

StrideThrough

A Sequence of values formed by striding over a closed interval.

StrideThroughIterator

An iterator for StrideThrough<Element>.

StrideTo

A Sequence of values formed by striding over a half-open interval.

StrideToIterator

An iterator for StrideTo<Element>.

String

A Unicode string value.

CharacterView

A view of a string’s contents as a collection of characters.

Index

A position in a string’s CharacterView instance.

UnicodeScalarView

A view of a string’s contents as a collection of Unicode scalar values.

Index

A position in a string’s UnicodeScalars view.

Iterator

An iterator over the Unicode scalars that make up a UnicodeScalarView collection.

UTF16View

A view of a string’s contents as a collection of UTF-16 code units.

Index

A position in a string’s collection of UTF-16 code units.

Indices
UTF8View

A view of a string’s contents as a collection of UTF-8 code units.

Index

A position in a string’s UTF8View instance.

UInt

An unsigned integer value type.

UInt16

A 16-bit unsigned integer value type.

UInt32

A 32-bit unsigned integer value type.

UInt64

A 64-bit unsigned integer value type.

UInt8

An 8-bit unsigned integer value type.

UnfoldSequence

A sequence whose elements are produced via repeated applications of a closure to some mutable state.

UnicodeScalar

A Unicode scalar value.

Unmanaged

A type for propagating an unmanaged object reference.

UnsafeBufferPointer

A non-owning pointer to a buffer of elements stored contiguously in memory, presenting a collection interface to the underlying elements.

UnsafeBufferPointerIterator

An iterator for the elements in the buffer referenced by UnsafeBufferPointer or UnsafeMutableBufferPointer.

UnsafeMutableBufferPointer

A non-owning pointer to a buffer of mutable elements stored contiguously in memory, presenting a collection interface to the underlying elements.

UnsafeMutablePointer

A raw pointer for accessing data of type Pointee. This type provides no automated memory management, and therefore must be handled with great care to ensure safety.

UnsafeMutableRawPointer

A raw pointer for accessing untyped data. This provides no automatic memory management, no type safety, and no alignment guarantees. This implements Strideable to provide a view of byte-addressable memory.

UnsafePointer

A raw pointer for accessing data of type Pointee. This type provides no automated memory management, and therefore must be handled with great care to ensure safety.

UnsafeRawPointer

A raw pointer for accessing untyped data. This provides no automatic memory management, no type safety, and no alignment guarantees. This implements Strideable to provide a view of byte-addressable memory.

UTF16

A codec for translating between Unicode scalar values and UTF-16 code units.

UTF32

A codec for translating between Unicode scalar values and UTF-32 code units.

UTF8

A codec for translating between Unicode scalar values and UTF-8 code units.

Zip2Iterator

An iterator for Zip2Sequence.

Zip2Sequence

A sequence of pairs built out of two underlying sequences.

Enumerations

CommandLine

Command-line arguments for the current process.

FloatingPointClassification

The IEEE 754 floating-point classes.

FloatingPointRoundingRule

A rule for rounding a floating-point number.

FloatingPointSign

The sign of a floating-point value.

ImplicitlyUnwrappedOptional

An optional type that allows implicit member access.

MemoryLayout

The memory layout of a type, describing its size, stride, and alignment.

AncestorRepresentation

Representation of ancestor classes.

DisplayStyle

A suggestion of how a Mirror’s subject is to be interpreted.

Never

The return type of functions that do not return normally; a type with no values.

Optional

A type that can represent either a wrapped value or nil, the absence of a value.

PlaygroundQuickLook

The sum of types that can be used as a Quick Look representation.

UnicodeDecodingResult

The result of one Unicode decoding step.

Reference

Swift Standard Library Functions

This chapter describes the global functions defined in the Swift standard library.

Swift Standard Library Operators

This chapter describes the operator declarations and corresponding global operator functions defined in the Swift standard library.

Swift Standard Library Type Aliases