Tuples in Swift

Tuples are a compound type in Swift, it can hold multiple values. In swift tuples are used to declare constants and it can be done with the help of the let keyword.

Source code :


// Creating a Tuple

let fruit = (name : "Apple", taste: "Sweet")

// Reading tuple values

print(fruit.name) //prints Apple

//Assiging tuples to variables

var tasteOfApple = fruit.taste

print("The Taste of Apple is " ,tasteOfApple) //The Taste of Apple is  Sweet

//Printing a Tuple

print(fruit) //prints ("Apple", "Sweet")

Output :

Apple
The Taste of Apple is  Sweet
("Apple", "Sweet")

Output :


                

Comments :