Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A tuple in Swift is a lightweight, finite, and ordered compound type that groups multiple values of potentially disparate types into a single entity. As a structural value type, a tuple is passed by copy, and its arity (number of elements) and element types are statically determined at compile time. Unlike nominal types (classes, structs, enums), tuples do not support methods, custom initializers, or protocol conformance.

Syntax and Initialization

Tuples are defined using a comma-separated list of types or values enclosed in parentheses. They can be instantiated with or without explicit element labels.
// Unnamed tuple with explicit type annotation
let unnamedTuple: (String, Int, Bool) = ("Compiler", 1, true)

// Named tuple with type inference
let namedTuple = (name: "Compiler", version: 1, isStable: true)

// Mixed tuple (some elements named, some unnamed)
let mixedTuple: (String, version: Int) = ("Compiler", 1)

Element Access

Tuple elements are accessed either by zero-based positional indices or by their explicit labels using dot notation.
// Index-based access (available for all tuples)
let systemName = unnamedTuple.0
let systemVersion = unnamedTuple.1

// Label-based access (available only for named elements)
let sysName = namedTuple.name
let sysStable = namedTuple.isStable

Decomposition (Destructuring)

A tuple’s contents can be extracted and bound to individual constants or variables in a single operation. The wildcard pattern (_) can be used to ignore specific elements during decomposition.
let configuration = (host: "127.0.0.1", port: 8080, secure: true)

// Full decomposition
let (hostAddress, portNumber, isSecure) = configuration

// Partial decomposition using the wildcard operator
let (address, _, secure) = configuration

Mutability

If a tuple is assigned to a variable (var), its individual elements can be mutated. However, the tuple’s structural signature—its arity and the specific types of its elements—is immutable.
var response = (code: 200, message: "OK")

// Valid mutation
response.code = 404
response.message = "Not Found"

// Invalid mutation (Compile-time error: Cannot assign value of type 'String' to type 'Int')
// response.code = "Error" 

Type Aliasing

Because tuples are structural types, complex tuple signatures can become verbose. The typealias keyword is used to assign a nominal identifier to a tuple structure for strict type checking and readability.
typealias HTTPResponse = (statusCode: Int, payload: String)

let fetchResult: HTTPResponse = (200, "{\"data\": []}")

Equality and Comparison

Tuples do not conform to protocols, meaning a tuple cannot be passed to a generic function constrained to <T: Equatable> or <T: Comparable>. However, the Swift standard library provides overloaded equality (==, !=) and comparison (<, <=, >, >=) operators for tuples up to an arity of 6, provided all constituent elements support these operations. Comparison is evaluated lexicographically (left-to-right).
let tupleA = (1, "apple")
let tupleB = (1, "banana")
let tupleC = (2, "apple")

let isEqual = (tupleA == tupleB) // false
let isLess = (tupleA < tupleB)   // true (1 == 1, but "apple" < "banana")
let isGreater = (tupleC > tupleA) // true (2 > 1, evaluation stops at the first element)
Note: Element labels are ignored during tuple equality checks. (a: 1, b: 2) is considered equal to (x: 1, y: 2).
Master Swift with Deep Grasping Methodology!Learn More