What Are “Tuples and Typealias”? | Swift Basics Tutorial

iOS tutorial SWIFT Tuples Typealias featured

In this iOS tutorial SWIFT Tuples Typealias, we’re going to show you what Tuples and Typealias are and how to use them through some great examples.

Tuples

By definition, tuples are a compound type in swift. This means that they group multiple values into one value. Think of tuples as like an email. We create one draft that has many parameters such as the sender, subject, message body, and attachments. All of these components make up a single email.

Here’s an example of a tuple in Swift:

let emailDraft = ( to: "MyBoss", subject: "I'm Sick", body: "Sorry Boss, but I'm feeling under the weather")

Now we can use one of the indexes to grab a single property from our constant named “emailDraft”. If we try to get the object “emailDraft.subject”, the console will return the following string: “I’m Sick”.

There are ways in which you can decompose tuples, which means to turn a tuple into multiple variables to get control of each individual value.

The following is an example of decomposing a tuple:

let(emailSender, emailSubject, emailBody) = emailDraft

The objects above will receive values according to their index.

It should also be noted that tuples give functions multiple return values, and below is an example of just that:

func getEmail(email:String) -> (sender:string, subject:string, body:string)
 
{
 
    return(sender:"Bob", subject:"Urgent News", body:"I just opened up a can of soda with my feet!")
 
}
 
let emailDraft = getEmail("compose")

Typealias

this is simply a shortcut to get on already existing type by giving it an alternate name. For my next example, I’m going to give the type “String” a typealias of “Message”.

typealias Message = String
 
var randomVar:Message = "Hello There"
 
print(randomVar)
 
The console will return: "Hello There"

In the above code, we simply created a substitute name for the type “String”. For now this seems like a waste of time, but typealiases prove to be quite useful in more advanced swift development by making code much cleaner to read.

Writing Loops With Ranges

Let’s see how we can write this loop with ranges:

for var i = 0; i < 10; i++
 
{
 
    print("Hi There"
 
}

We have two options here: We can either used the closed operator, or the half open operator. The closed operator will produce all the integers from the range we give it, and the half open one will do the same thing except commit the final integer.

The loop with ranges can be seen here:

Closed Operator Version:

for _ in 0...10
 
{
 
    print("Hi There")
 
}

Half-Open Operator Version:

for _ in 0.. <11
 
{
 
    print("Hi There")
 
}

Both of the loops above will produce the integers 0-10, the only discrepancy is that they are written differently with two different uses of range operators.

In conclusion, if you liked this iOS tutorial SWIFT Tuples Typealias, take a look at some of our other stuff at the tutorial section.

Recommended for you [SWIFT] How to add In-App Purchases in your iOS app

Leave a Reply

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