0Pricing
Swift Academy · Lesson

Tuples

Tuples

Tuples is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Hello,

It is a type that is used to keep more than one value in a combined form. Consider a function, this function freezes the latitude and longitude information of a coordinate.

It would be more logical to keep these two values ​​associated with each other combined in an advanced type such as Tuple instead of separate return values.

Warning

The values ​​passed to the tuple do not have to be of the same type. Such as (Int, String).

example

let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), 
// and equals (404, "Not Found")

let coordinates = (26, 45)
let datas = (1,2,3,4)
// The values ​​in the tuple don't have to be just 2. 
// A group can be created from more than one number of values.

Destructure

Destructure Tuple

http404Error we defined in previous code.

We can destructure this tuple into its data as follows. 

Let's look at our example.

let http404Error = (404, "Not Found")

let (statusCode, statusMessage) = http404Error
// In the example below, 
//String concatenation feature is seen.
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found"

underscore operator

If we do not need some values ​​of the tuple, these values ​​can be ignored using the underscore operator.

let http404Error = (404, "Not Found")

// In the example below, since the string 
//Not Found is not needed, the corresponding 
//expression is passed with _ operator
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"

How to get easy access to tuple data?

How to get easy access to tuple data?

Access to data in tuple data can be done with index. 

In the example below .0 refers to the first value, while 1 refers to the second value.

Let's take a look.

let http404Error = (404, "Not Found")

print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"

Naming tuple value

Tuple values ​​can be assigned names

let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is \(http200Status.description)")
// Prints "The status message is OK"

Tuples can be used especially as return types.

Tuples can be used especially as return types.

Tuples are not suitable to use for complex data structures. If data starts complexing, I recommend using a struct or data class.

Congratulations

Congratulations 🎉

In this lesson, we learned how to use tuples in Swift.

See you in the next lesson.

Tuples — illustration 9

Frequently asked questions

Is the “Tuples” lesson free?

Yes — the full text of “Tuples” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Tuples”?

Tuples You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Tuples” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to Swift Academy