Swift Tuples
Tuples are grouping of multiple elements into single compound values. That is a combination of index and value pair, and also possible to define in simple form. We can define any type of data element in tuples fields. For example.
//Define a tuple key-value
var wallet=(dollar:"1000 $",rupyee:"69390 ₹",euro:889.16)
//Define a tuple only value
var swiftType=("Swift 2","Swift 3","Swift 4")
Tuples elements are defined of within the parenthesis. separate by comma. We can define any number of tuples elements, In this case given of three elements.
Note that index value in this case a string and that are not surrounded by double quotes. But value is string then put inside double quotes.
Access of tuple elements
There are many way to access tuple element in Swift programming. first is simple form, use element by given index.
//Define a tuple
var wallet=(dollar:"1000 $",rupyee:"69390 ₹",euro:889.16)
print("Dollar : \(wallet.dollar)")
print("Euro : \(wallet.euro)")
print("Rupyee : \(wallet.rupyee)")
Output
Dollar : 1000 $
Euro : 889.16
Rupyee : 69390 ₹
In this example using if dot and index value get tuple element. suppose in case we are not about that the name of tuple index. But sequence of element are know. then we can use numbering index value.
//Define a tuple
var days=(sun:"Sunday",mon:"Monday",tue:"Tuesday",wed:"Wednesday",thur:"Thursday",frid:"Friday",sat:"Saturday")
print("\(days.0)") //Sunday
print("\(days.4)") //Wednesday
print("\(days.6)") //Saturday
Index of tuple is start of zero. and increment one by one form left to right. In this tuple are 7 elements but index are represent between (0-6).
Suppose in case we are need to display all tuple element then do following way.
//Define a tuple
var days=(sun:"Sunday",mon:"Monday",tue:"Tuesday",wed:"Wednesday",thur:"Thursday",frid:"Friday",sat:"Saturday")
let temp = Mirror(reflecting: days).children
//display tuple elements
for day in temp {
print("\(day.value) ")
}
Output
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
In case tuple is form of label and values then we can printed in following value to that label and values.
var days=(sun:"Sunday",
mon:"Monday",
tue:"Tuesday",
wed:"Wednesday",
thur:"Thursday",
frid:"Friday",
sat:"Saturday")
var element = Mirror(reflecting: days).children
for i in element {
print("\(i.label!) : \(i.value)")
}
Output
sun : Sunday
mon : Monday
tue : Tuesday
wed : Wednesday
thur : Thursday
frid : Friday
sat : Saturday
When we need to count element of tuples then we can find this ways.
var numbers=(0,1,2,3,4,5,6,7)
//Count number of elements in tuples
var size = Mirror(reflecting: numbers).children.count
print("Size of This tuple : \(size)") //8
Output
Size of This tuple : 8
Decomposing tuple elements
let elements=("Swift","Kotlin","Java")
let (first,second,third) = elements
print("First : \(first)")
print("Second : \(second)")
print("Third : \(third)")
Output
First : Swift
Second : Kotlin
Third : Java
Modify tuple elements
Modification are possible of tuple elements. We can modified tuple element by element position of using dot operator.
//Define a tuple
var coins=(1,2,3,4,5)
print("Before Modified : \(coins)")
coins.0=10
coins.4=50
print("After Modified : \(coins)")
Output
Before Modified : (1, 2, 3, 4, 5)
After Modified : (10, 2, 3, 4, 50)
Where to use tuples
This are generally used to when function is returning multiple value of multiple types. See this example.
/*
* func: uniqueData
* parm: One integer array
* return : tuples
*/
func uniqueData(data:[Int])->(uniue:[Int],duplicates:[Int],repeated:Int){
//make empty array
var uniqueElement=[Int]()
var duplicatesElement=[Int]()
var repeated:Int=0
for element in data{
//check array element is duplicate or not
if uniqueElement.contains(element){
repeated+=1
duplicatesElement.append(element)
}else{
uniqueElement.append(element)
}
}
//return single tuples
return (uniqueElement,duplicatesElement,repeated)
}
let myElement=uniqueData(data:[1,1,7,2,1,8,2,3,7])
print("Returning values :\(myElement)")
print("Uniue Element : \(myElement.uniue)")
print("Duplicates Element : \(myElement.duplicates)")
print("Total duplicates : \(myElement.repeated)")
Output
Returning values :(uniue: [1, 7, 2, 8, 3], duplicates: [1, 1, 2, 7], repeated: 4)
Uniue Element : [1, 7, 2, 8, 3]
Duplicates Element : [1, 1, 2, 7]
Total duplicates : 4
In this example uniqueData() function are return a tuples which are contain 2 arrays and 1 integers. We can also used tuples in mathematical operations. For example swapping two variable values.
//Swapping two number by using of tuples
var num1:Int=10
var num2:Int=20
print("Before Swapping :")
print("num1: \(num1) num2: \(num2)")
(num1,num2)=(num2,num1)
print("After Swapping :")
print("num1: \(num1) num2: \(num2)")
Output
Before Swapping :
num1: 10 num2: 20
After Swapping :
num1: 20 num2: 10
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment