Skip to content
Prepare to Grow

Swift Fundamentals

Closures

Pass behavior as values, transform collections, and understand captured context.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Read and write closure expressions.
  • Pass closures to collection methods.
  • Use trailing closure syntax.
  • Explain what it means for a closure to capture a value.
01

Learning stage

Concept Explanation

A closure is a self-contained block of functionality that can be stored and passed around. Functions are named closures; closure expressions provide a compact unnamed syntax.

Closures can capture constants and variables from their surrounding scope. This makes them powerful for callbacks and transformations, but captured references require care in longer-lived asynchronous or stored closures.

  • { parameters in statements } is the basic expression shape.
  • map transforms each element and returns a new collection.
  • filter keeps elements matching a condition.
  • sorted(by:) accepts comparison behavior.
02

Learning stage

Syntax & Examples

The example filters and transforms lesson values with trailing closures.

LessonClosures.swiftswift
let minutes = [8, 15, 12, 20]

let longerLessons = minutes.filter { value in
  value >= 12
}

let labels = longerLessons.map { "\($0) min" }
let total = minutes.reduce(0) { partial, value in
  partial + value
}

print(labels)
print("Total: \(total) min")
Expected labels are 15, 12, and 20 minutes; the total is 55 minutes.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Handle button actions and completion callbacks.
  • Transform, filter, sort, and group model collections.
  • Configure APIs by passing a small piece of behavior instead of creating a new type.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Compressing a multi-step closure until its meaning is hard to read.
  • Using shorthand arguments when names would clarify several values.
  • Creating strong reference cycles when an escaping closure captures an owning object.
  • Using nested completion handlers where structured async code would be clearer.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Sort lesson titles

Sort an array of lesson titles by character count, placing shorter titles first.

Hint: sorted accepts a closure comparing two elements.

Activity 2

Observe a captured value

Create a variable outside a closure, update it inside a closure, and explain why the closure can access it.

Hint: Start with a synchronous closure so lifecycle and memory are not distractions.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What does map return?

Reveal answer

A new collection containing the result of transforming each source element.

QUESTION 2

What does it mean for a closure to capture a value?

Reveal answer

The closure keeps access to a constant or variable from its surrounding context.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

What is a closure in Swift?

Short answer: A closure is a block of executable behavior that can be stored, passed, and invoked as a value.

Explore the detailed answer

Swift functions are closures with names, while closure expressions are unnamed. Closures have parameter and result types, can use trailing syntax, and can capture values from their lexical scope. Stored escaping closures may require deliberate capture semantics.

Possible follow-ups

  • • What is a trailing closure?
  • • What can cause a retain cycle?
08

Learning stage

Key Takeaways

Closures let APIs receive behavior. Keep them readable, use collection transformations where they express intent, and treat capture lifetime as part of the design.

  • Closures are function values.
  • Trailing syntax improves many call sites.
  • map, filter, and reduce return new results.
  • Captured references can affect object lifetimes.