0Pricing
Swift Academy · Lesson

Closures & Capture basics

Understand closure syntax, shorthand args, trailing closure, capture lists, and escaping vs non-escaping.

Intro

This lesson introduces closures: self-contained blocks of code you can pass around. Explore syntax, shorthand args, trailing closure, capture lists, and escaping vs non-escaping.

Closure syntax

Closures look like lightweight functions. Syntax: { (params) -> Return in ... }.

let greet = { (name: String) -> String in
    return "Hello, \\(name)"
}
print(greet("Ada"))

All lessons in this course

  1. Closures & Capture basics
  2. Capture lists — weak/unowned & value capture
  3. Escaping vs non-escaping, autoclosures
← Back to Swift Academy