Tuples

Tuples bundle a handful of values into one. By the end of this lesson you'll group values, name and decompose their elements, and return multiple values from a function — Swift's lightweight alternative to a one-off struct.

Learn Tuples in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Grouping Values

A tuple combines several values — possibly of different types — into one. Access elements by position with .0 , .1 , and so on (zero-based).

2️⃣ Named Elements

Give elements names for readability — (x: 3, y: 5) — then read them by label like point.x . Positional access still works too.

3️⃣ Decomposition

Unpack a tuple's elements into separate constants in one statement. Use _ to skip parts you don't need. This pattern is everywhere in Swift loops over pairs.

4️⃣ Returning Multiple Values

A function can return a tuple to hand back several related results at once — no extra type required. The caller reads them by label or decomposes them.

Your turn. Fill in the blanks, then run it and check the output.

Pro Tips

Common Errors (and the fix)

📋 Quick Reference

Task

Code

Result

Create

let t = ("Ada", 36)

(String, Int)

By position

t.0

first element

Named

(x: 3, y: 5).x

3

Decompose

let (a, b) = t

a, b separately

Ignore

let (_, b) = t

skip first

Return many

-> (min: Int, max: Int)

two values

Frequently Asked Questions

Mini-Challenge: Divide with Remainder

Build it, run it, and check your output against the example in the comments.

🎉 Lesson Complete!

Practice quiz

What is a tuple in Swift?

  • An array of one type
  • A key-value map
  • A group of multiple values combined into one compound value
  • A kind of optional

Answer: A group of multiple values combined into one compound value. A tuple groups several values, which can be of different types, into one value.

Can the elements of a tuple have different types?

  • Yes, e.g. (Int, String)
  • No, they must match
  • Only two same-type values
  • Only with classes

Answer: Yes, e.g. (Int, String). Tuple members may each have a different type.

How do you access the first element of an unnamed tuple ?

Unnamed tuple members are accessed by position with .0, .1, etc.

Given let point = (x: 3, y: 5), how do you read the x value?

  • point.0 only
  • point.x

Answer: point.x. Named tuple elements are accessed by their label, e.g. point.x.

What does do?

  • Decomposes the tuple, binding a=1 and b=2
  • Creates a dictionary
  • Raises an error
  • Creates an array

Answer: Decomposes the tuple, binding a=1 and b=2. This is tuple decomposition: each element is bound to a name.

What is a common use of tuples?

  • Storing thousands of items
  • Replacing classes
  • Returning multiple values from a function
  • Looping forever

Answer: Returning multiple values from a function. Tuples let a function return several related values at once.

If a function returns (min: Int, max: Int), how do you read the max?

  • result.maximum
  • result.1 or result.max

Answer: result.1 or result.max. You can use the label result.max or the position result.1.

What is the index of the second element of an unnamed tuple?

  • .2

Positions are zero-based, so the second element is .1.

Can you ignore a tuple element during decomposition?

  • No
  • Yes, using an underscore _ in its place
  • Only the first one
  • Only named ones

Answer: Yes, using an underscore _ in its place. Use _ to ignore an element you don't need, e.g. let (_, y) = point.

Are tuples best for large, long-lived data models?

  • Yes, always prefer them
  • Yes, they replace structs
  • No — for richer/persisted data, prefer a struct
  • Only for networking

Answer: No — for richer/persisted data, prefer a struct. Tuples are great for lightweight grouping; structs are better for substantial models.

Continue this course

Related lessons