- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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