2
2
.
.
4
4
.
.
1
1
T
T
u
u
p
p
l
l
e
e
s
s
I
I
n
n
f
f
o
o
Tuple is Data Type that contains list of Variables which can be
of different Data Types
accessed through their names or indexes
If Tuple is declared with named Variables list of values can be given in any order.
Tuple can't have name and can only have Variables (can't have Methods).
Tuple can be stored in a Constant or Variable that is declared to accept that specific definition of a Tuple.
Tuples are used when we want to move around multiple Variables but we want to avoid declaring separate Class or Struct
T
T
u
u
p
p
l
l
e
e
S
S
y
y
n
n
t
t
a
a
x
x
Tuple Data Type is declared
inside rounded brackets
which contains a list of named Parameters and their Data Types (status: Int, message: String)
Test.swift
//DECLARE TUPLES.
var http404 = (404 , "Not Found") //Implicit data types.
var http505:(status: Int, message: String) = (505 , "redirected") //Explicit data types. Same order.
var http201:(status: Int, message: String) = (message: "OK", status: 201) //Explicit data types. Named
values
//REFERENCE TUPLE VALUES.
print(http404) //(404, "Not Found")
print(http505) //(status: 505, message: "redirected")
print(http201) //(status: 201, message: "OK")
print(http201.0) //201 Reference value using index.
print(http201.message) //OK Reference value using name.
//EXTRACT TUPLE VALUES.
var (status, message) = http201 //Store Tuple Parameters into Variables
print(status) //Refernce extracted value.
print(message) //Refernce extracted value.