Chapter 8 : Swift Tuples



As per definition of english word Tuple – A data structure consisting of multiple parts.
So tuples are used to group multiple values in a single value.Its of any type.

Syntax :

var Tuple_name = (Tuple_value1, Tuple_value2, Tuple_value1….to any number of values)
For Example :
1) Single value tuple
var errorType = (404) 
we can access the values of tuple using the index numbers that start from 0 like we did in array.
print(“The code is\(errorType.0)”)
Output - 404
2) Double value tuple
var errorType = (404,”Page not found”)
print(“The code is\(errorType.0)”)
Output - 404
print(“The code is\(errorType.1)”)
Output - Page not found
3) Triple value tuple
var errorType = (404,”Page not found”,”www.9to5ios.com”)
print(“The code is\(errorType.0)”)
Output - 404

print(“The code is\(errorType.1)”)
Output - Page not found

print(“The code is\(errorType.2)”)
Output - www.9to5ios.com

Use of Tuple :

Tuples are used if we need to return multiple values from a method or function and used for temporary values not for complex data.
For example of type (Int , “String”)

 



Add a Comment

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