Swift Tuples
A tuple in Swift groups multiple values into one compound value. The values may have different data types, and you can access them by position or by label.
Tuples are useful when a small set of related values belongs together but creating a dedicated structure would add unnecessary complexity. They are commonly used for temporary values, function return values, and lightweight data grouping.
The following is an example of Swift Tuple stored in a variable named player.
var player = (45, "Rohit")
The Tuple player has two values. The first is an Int and the second is a String. We can have a mix of any datatypes in a single Tuple.
Swift Tuple Type Syntax
A tuple type is written inside parentheses, with each element type separated by a comma.
var value: (Type1, Type2, Type3)
For example, the following tuple contains an Int, a String, and a Bool.
var employee: (Int, String, Bool) = (101, "Anita", true)
print(employee)
Output
(101, "Anita", true)
Accessing Swift Tuple Values by Index
We can access a tuple using the dot operator and index.
var player = (45, "Rohit")
var jersyNumber = player.0
var name = player.1
Tuple indices start at 0. Therefore, player.0 returns the first value and player.1 returns the second value.
let player = (45, "Rohit")
print(player.0)
print(player.1)
Output
45
Rohit
Named Swift Tuple Elements
We can also have names for the values inside tuple, and access those values using names.
var player = (jersyNumber: 18, name: "Kohli")
var jersyNumber = player.jersyNumber
var name = player.name
Labeled tuple elements make code easier to read because each value describes its purpose. The labels are part of the tuple type.
let product = (id: 501, name: "Keyboard", inStock: true)
print(product.id)
print(product.name)
print(product.inStock)
Output
501
Keyboard
true
Updating Values in a Swift Tuple
We can assign new values to the elements in a Tuple by accessing them using dot operator and assigning a new value.
var player = (1, "Rohit")
player.0 = 45
A tuple must be declared with var before its elements can be changed. A tuple declared with let is constant, so none of its values can be reassigned.
var coordinates = (x: 10, y: 20)
coordinates.x = 15
coordinates.y = 25
print(coordinates)
Output
(x: 15, y: 25)
Destructuring a Swift Tuple into Variables
Tuple decomposition, also called destructuring, assigns each tuple element to a separate variable or constant.
let player = (45, "Rohit")
let (jerseyNumber, playerName) = player
print(jerseyNumber)
print(playerName)
Output
45
Rohit
Use an underscore when you do not need one of the tuple values.
let response = (statusCode: 200, message: "OK")
let (_, message) = response
print(message)
Output
OK
Returning Multiple Values with a Swift Tuple
A Swift function can return several related values as a tuple. Labels make the returned values self-explanatory at the call site.
func calculate(_ a: Int, _ b: Int) -> (sum: Int, product: Int) {
return (a + b, a * b)
}
let result = calculate(4, 5)
print(result.sum)
print(result.product)
Output
9
20
Comparing Swift Tuples
Tuples can be compared when they have the same number of elements and each corresponding element supports comparison. Swift compares tuple values from left to right.
let first = (1, "Apple")
let second = (1, "Banana")
print(first == second)
print(first < second)
Output
false
true
In the second comparison, the first elements are equal, so Swift compares "Apple" and "Banana".
Swift Tuple Limitations and When to Use a Struct
Tuples work best for small, temporary groups of related values. They do not support stored methods, computed properties, protocols, inheritance, or custom initialization.
Use a struct instead when the data represents a reusable model, needs validation or behavior, appears in several parts of the program, or requires clearer type identity.
struct Player {
var jerseyNumber: Int
var name: String
}
let player = Player(jerseyNumber: 45, name: "Rohit")
print(player.name)
Output
Rohit
More Swift Tuple Tutorials
- Swift – Initialize a Tuple
- Swift – Get Element of a Tuple at Specific Index
- Swift – Update Element of a Tuple at Specific Index
- Swift – Initialize a Tuple with Labels for Values in it
- Swift – Access Values of a Tuple using Labels
Swift Tuple Questions
Can a Swift tuple contain different data types?
Yes. Each element in a tuple can have a different type, such as Int, String, Bool, or another tuple.
How do I access values in a Swift tuple?
Use zero-based positions such as tuple.0 and tuple.1, or use element labels such as tuple.name when the tuple was created with labels.
Can a Swift tuple return multiple values from a function?
Yes. Declare the function return type as a tuple and return all values together. Adding labels to the tuple makes each returned value easier to access.
What is the difference between a Swift tuple and a struct?
A tuple is an anonymous grouping of values and is best for short-lived data. A struct defines a reusable named type that can include properties, methods, initializers, and protocol conformance.
Can I add or remove elements from an existing Swift tuple?
No. A tuple has a fixed number of elements and fixed element types after its type is established. Create a new tuple when a different shape is required.
Swift Tuple Summary
In this Swift Tutorial, we learned to initialize, access, modify, destructure, compare, and return Swift Tuples with examples. Tuples are suitable for small groups of related values, while structs are a better choice for reusable data models.
TutorialKart.com