Swift – Tuples


Swift – Tuples



”;


Tuples are used to store a group of values in a single value, for example (32, “Swift Programming”) is a tuple with two values that are 23 and “Swift Programming”. Tuples can store multiple values and each value is separated by a comma. It can store values of the same or different data types. They are helpful when we want to return multiple values together.

Tuples are commonly used by the functions to return multiple values at the same time. Tuples are not used for complex data structures; they are useful for simple groups of related data.

Syntax

Following is the syntax of the Tuple −


var myValue = (value1, value2, value3, value4, …, valueN)

We can access the elements of the tuple with the help of dot notation followed by the element’s index −


var result = tupleName.indexValue

Example

Swift program to create and access the elements of the tuple.


import Foundation

// Creating a tuple
var myTuple = ("Romin", 321, "Delhi")

// Accessing the elements of Tuple
var name = myTuple.0
var id = myTuple.1
var city = myTuple.2

// Displaying the result
print("Employee name =", name)
print("Employee id =", id)
print("Employee city =", city)

Output


Employee name = Romin
Employee id = 321
Employee city = Delhi

Tuple with different Data types

In a tuple, we are allowed to store data of different types like (Int, Int, Float), (Float, Double, String), etc. You can also store data of the same data type like (Int, Int, Int), etc.

Example

Swift program to create a tuple of different data types.


import Foundation

// Creating a tuple with data of different data types
var myTuple = ("Mona", 21, "Mumbai", 101)

// Accessing the elements of Tuple
var name = myTuple.0
var age = myTuple.1
var city = myTuple.2
var id = myTuple.3

// Displaying the result
print("Student name =", name)
print("Student age =", age)
print("Student city =", city)
print("Student id =", id)

Output


Student name = Mona
Student age = 21
Student city = Mumbai
Student id = 101

Assigning Tuple to Separate Variables

We can assign the value of a tuple to a separate constant or variable so that we can access them using the name of that constant or the variable. Here the count of the constant or variable should be equal to the tuple’s values.

Syntax

Following is the syntax of assigning tuple values to a separate constant or variable −


let (name1, name2) = tuple

Example

Swift program to create a tuple whose values are assigned to a set of constants.


import Foundation

// Creating a tuple
var myTuple = ("Mickey", 21, "Pune")

// Assigning tuple to a set of constants
let(name, age, city) = myTuple

// Accessing the value of tuples according to the constant
print("Student name =", name)
print("Student age =", age)
print("Student city =", city)

Output


Student name = Mickey
Student age = 21
Student city = Pune

Tuple with Underscore in Swift

We are allowed to use an underscore “_” with a tuple. It will ignore the part of the tuple with an underscore while decomposing the tuple. Or in other words, we can say that by using underscore we can ignore some tuple’s values. We are allowed to use multiple underscores with the same tuple to ignore multiple values.

Syntax

Following is the syntax of a tuple with an underscore −


let (name1, name2, _) = tuple

Example

Swift program to create a tuple with underscore “_”.


import Foundation

// Creating a tuple
var myTuple = (21, "Pune", "CSE")

// Assigning a tuple to a set of constants
let(age, _, branch) = myTuple

// Accessing the values of tuples 
print("Student age =", age)
print("branch =", branch)

Output


Student age = 21
branch = CSE

Assigning Names to Individual Values in a Tuple

In Swift, we are allowed to assign names to the individual elements of the tuple. We can also access the tuple’s elements according to their names.

Syntax

Following is the syntax for assigning names to a tuple’s elements −


let myTuple = (id: 102, name: "Sona")

Example

Swift program to create and access tuple elements according to their names.


import Foundation

// Creating a tuple
var myTuple = (name: "Mona", branch: "ECE", year: 2022) 

// Accessing the values of tuples according to their names
print("Student name =", myTuple.name)
print("Branch =", myTuple.branch)
print("Current Year", myTuple.year)

Output


Student name = Mona
Branch = ECE
Current Year 2022

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *