Contents

Swift Tuples

A tuple in Swift is a constant or variable that can hold a group of values, which can be of different data types, combined into a single value. In simpler terms, a tuple is a structure that allows multiple values of various types to be stored together. Tuples are often used as return values to retrieve multiple data points from a function or process.

Creating a Tuple in Swift

To create a tuple, declare a constant or variable and define the data inside parentheses () separated by commas. The elements can be of the same or distinct data types.

Example 1: Declaring a Tuple Containing a String and an Integer

				
					// statusLoad is of type (String, Int) and equals (“Forbidden”, 403)
let statusLoad = ("Forbidden", 403)

				
			

Here, the tuple statusLoad holds two values:

  • A String providing a human-readable description ("Forbidden")
  • An Int representing a numeric code (403)

Example 2: Declaring Tuples with Multiple Data Types

				
					let a = (1, 2, 3, 4)
var b = ("Hello", 42, "Swift", 7, 99)

				
			

Tuples can also have named elements for better readability.

Example 3: Naming Elements Inside a Tuple

				
					let statusLoad = (tupleMessage: "Forbidden", tupleCode: 403)

				
			
Decomposing a Tuple

You can access tuple elements by decomposing the tuple into variables or constants, as shown below:

				
					// Refer to statusLoad from above
let (sMessage, sCode) = statusLoad

// Print the message
print("The status message is \(sMessage).")

// Print the code
print("The status code is \(sCode).")

				
			

Output:

				
					The status message is Forbidden.
The status code is 403.

				
			

Example 4: Accessing Tuple Elements Using Indexes

You can access elements in a tuple using their index.

				
					let person = ("John", 25, "Developer")

// Accessing elements by index
print("Name: \(person.0)")
print("Age: \(person.1)")
print("Profession: \(person.2)")

				
			

Output:

				
					Name: John
Age: 25
Profession: Developer

				
			

Example 5: Naming Tuple Elements and Accessing Them

Named elements make accessing tuple values more intuitive.

				
					let book = (title: "Swift Programming", author: "John Doe", pages: 350)

// Accessing elements by name
print("Book Title: \(book.title)")
print("Author: \(book.author)")
print("Number of Pages: \(book.pages)")

				
			

Output:

				
					Book Title: Swift Programming
Author: John Doe
Number of Pages: 350

				
			

Example 6: Returning a Tuple from a Function

Tuples are commonly used as return types for functions when you want to return multiple values.

				
					func getDeviceInfo() -> (name: String, price: Double, inStock: Bool) {
    return ("iPhone", 999.99, true)
}

let device = getDeviceInfo()

print("Device Name: \(device.name)")
print("Price: \(device.price)")
print("In Stock: \(device.inStock)")

				
			

Output:

				
					myArray: ["Swift", "iOS", "Xcode", "iPad"]

				
			

Example 7: Using a Tuple in a For Loop

You can use tuples to iterate over key-value pairs in a collection.

				
					let scores = [("Alice", 90), ("Bob", 85), ("Charlie", 95)]

for (name, score) in scores {
    print("\(name) scored \(score) marks.")
}

				
			

Output:

				
					Alice scored 90 marks.
Bob scored 85 marks.
Charlie scored 95 marks.