Beauty of Tuple in Swift

Ekramul Hoque
Good Morning Swift
Published in
3 min readJun 5, 2018

Tuple is Another Small Version of Struct

What is Tuple in Swift :

Tuple is a compound type in swift which can hold multiple type .And Very Simply tuple is a type which can hold zero or more type. like one tuple can hold Int String bool or any others data even it can be hold others compound type and name type .

In apple Documentation:

A tuple type is a comma-separated list of zero or more types, enclosed in parentheses.

Something more important to understand that tuple is value type not reference type .

Syntax :

var (name of tuple)  = (type1,type2)var myTuple = (100,"String") // here tuple have 2 value Int and String

Tuple can create two way implicitly defined type or explicitly defined :

Explicitly defined :

var tupleOne:(Int,String) = (12,"Ekram")

Here We defined tuple type explicitly (Int,String)

Explicitly Defined Type :

var tupleTwo = ("Ekram",true)

Here two element on this tupleTwo one is string and another is bool

Create with type Name:

We can defined type element name also like this

var tupleTwo = (name:"Name",isOky:true)

Access tuple :

We can access tuple element by two way

  1. By Name

Example:

var tupleTwo = (name:”Name”,isOky:true)

Now we can access both type by name like this

var name = tupleTwo.namevar nameIsOky = tupleTwo.isOky
  1. By index

We can access tuple element by their index like this

Create a tuple

var tupleThree = (200,"sucess")

access by index :

var code = tupleThree.0var massage = tupleThree.1

Here we didn’t named the type so we can access tuple compound type by index .

Multiple assignment to multiple value:

We can assign multiple value to multiple tuple and access its separately like this:

var (name,age,color,height) = ("Asagullah","28","black",56)

Copy:

When we copy a tuple to another tuple its actually create unique copy of its cos its value type

Create a tuple and copy it to another tuple

var fullName = (firstName:"Asadullah",middleName:"Al",lastName:"Galib")var userFullName = fullName // now 

now userFullName is completely another tuple

We can access this like others tuple we access .

userFullName.firstNameuserFullName.lastNameuserFullName.middleName

Advanced Tuples

Tuple inside in a Tuple :

var anotherTuple:(Bool,(String,Int)) = (ok:true,(name:”Asagullah”,age:27))

Here we create an tuple called anotherTuple and its hold two type Bool and (String,Int) here is second type have also another tuple is string and Int

Lets another Example with bit complex way :

var anotherTwo:([String:Int],Bool,(String,String),[Int]) = (["Code":200],true,("asadullah","al galib"),[123,678])

Here is anotherTwo Tuple contain

  1. a dictonaray
  2. a Bool
  3. a Tuple
  4. a Array

So here its clear than we can store any type in a tuple its more than flexible .

Use Of Tuple :

Tuple can use most of the type in returning multiple value from a function or sending value by parameter .

Example

func backMeFullname(first:String,last:String)->(String,String) {                var fullName = (firstName:first,lastName:last)              return fullName
}
var myFullName = backMeFullname(first: "Ekramul", last: "Hoque")
print("My First Name : \(myFullName.0)")

Here backMeFullName function return a tuple (String,String).

Now Lets Create a typealias

typealias myCustomType = (Int,String)func takeaTuple()->myCustomType{          var age:Int = 28
var name:String = "hasan"
var nameAndAge:myCustomType = (age,name)
return nameAndAge
}
let hisNameAge = takeaTuple()print(hisNameAge.0) //
print(hisNameAge.1) //

We can even store class ,struct, enum type also in Tuple like this

typealias myType = (MyClass,MyStruct,AnotherClass)let classTyple:myType = (MyClass(),MyStruct(),AnotherClass())

Its Good to not use complex Tuple

Conclusion :

I think one of the pretty beautiful feature of swift is Tuple.If you like this article please give me clap and share it for another learner.

--

--