Swift tuple & typealias

tuple & typealias, the little buddies on Swift



Tuple may seems like a non-important topic to talk about on Swift. But smart dudes knows the benefit of using tuple. Mixing tuple with typealias on Swift can make our code more joyful to our coworker. So let us dive deep.

Background

Who are you tuple

tuple is just a group of types. What types? any types such as Int, String etc even custom type. Any limitation on number of type? Nope there is none.

let greetings = ("Hola", "Spanish")
greetings.0 // "Hola"

On the above ("Hola", "Spanish") is a tuple. Here it is constructed with two String. Simple right?

tuple declaration and usage

There are two ways of creating the tuple.

Without any name of the elements or types. As we saw on the previous section’s example:

let greetings = ("Hola", "Spanish")

Here greetings has a tuple of type (String, String). As the individual String type does not have a name, we have to access them by using the chronology of their appearance on the tuple type. So to access the “Spanish” we have to use the index 1.

greetings.1 / "Spanish"

On the other hand we can use some kind of name on each of the elements or type of a tuple.

let organisedGreetings = (phrase: "Hej", locale: "Swedish")
organisedGreetings.phrase //"Hej"

Now we can access the Hej by using the name, phrase, of the tuple type. Though We will loose the ability to use the index, as we did on the previous greetings, if we use the name typed tuple. But names on tuple have more code readability.

When to use

Well there can be two common place to use tuple.

  • When we need to group multiple types in a single place
  • when a function needs to returns multiple Types.
func info()->(name: String, age: Int){
    return ("mobidevtalk", 0)
}

let mobiDevTalkInfo = info()
mobiDevTalkInfo.name
mobiDevTalkInfo.age

Here the function info() is returning a Tuple.

Why to use

The reason is very simple:

  • Simplicity
  • Lightweight
  • Clarity

Let us consider the function info() on the previous section. So if we did not use the tuple then we have to find some other alternative way, like creating another Type, like as following:

struct Info{
    let name: String
    let age: Int
}

func modifiedInfo() -> Info{
    return Info(name: "mobidevtalk", age: 0)
}

As we can see it is an overhand of a simple task when we do not use the tuple.

typealias with tuple

Sometimes we mix tuple with typealias. But those two are entirely different and used for two separated purpose. So what is typealias?

typealias is a redeclaration of an existing type, more appropriately named alias.

typealias Amount = Int
typealias Execute = (Int)->(String)

As we can see the Amount is representing Int. But if we are working with some financial concepts then the Amount makes more sense than the Int. Similarly Execute also a named alias of a function.

Now those simple example can not infer the importance of typealias.

typealias with tuple can makes our code more concise with better readability and simplicity. Example please:

typealias Age = (month: Int, year: Int)

struct Profile{
    let name: String
    let age: Age
    
}

let profile = Profile(name: "mobidevtalk", age: Age(month: 4, year: 0))
profile.age.month // 4

let shortProfile = Profile(name: "mobidevtalk", age: (4, 0))
shortProfile.age.year // 0

As we can see the Age is a typealias of the tuple (month: Int, year: Int). Later down the code we are using Age as the alternative of that tuple. At the same time the tuple is holding a combined type of (Int, Int). So those two concepts are making the code more concise. Giving us a far more better readability and simplicity in our current, profile based context.

Difference from an Array

This may not be an obvious topic, but let us face it. Array is a collection type of object. tuple is also a group of objects. So are they similar? Ha ha, silly question.

No they are not the same. Array is a collection of same typed object. So all the elements of an array must be the same type, either all need to be Int or String or any other of same type.

On the other hand tuple is obviously a group of types but it is definitely not a collection of objects.

End talk

So this was our small talk on tuple & typealias on Swift. As always all the codes are available on GitHub.

We also have some talks on enum. All the talks over enum are available on Enum Hub.

Hope to see you in our next talk. Till then Happy talking.

Leave a Reply

Notifications for mobidevtalk! Cool ;) :( not cool