Swift
programming
closures
Swift closures
$0 $1 in Swift

What does 0 and 1 mean in Swift Closures?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Swift closures, $0, $1, $2, and so on are shorthand names for the closure's parameters. They let you write small closures without explicitly naming the arguments, which is convenient when the meaning is obvious from context.

What $0 and $1 Refer To

The numbering is positional:

  • '$0 is the first parameter'
  • '$1 is the second parameter'
  • '$2 is the third parameter'

For example, in a closure passed to sorted, there are two arguments being compared:

swift
1let numbers = [3, 1, 2]
2let sortedNumbers = numbers.sorted { $0 < $1 }
3
4print(sortedNumbers)  // [1, 2, 3]

Here, $0 and $1 are the two elements that sorted asks the closure to compare.

Why the Syntax Exists

Swift closures can be written in a very compact style. If you had to name every argument every time, short collection transformations would become noisy.

Compare these two versions:

swift
1let names = ["ana", "bob", "carla"]
2
3let uppercasedA = names.map { name in
4    name.uppercased()
5}
6
7let uppercasedB = names.map {
8    $0.uppercased()
9}

Both produce the same result. The second version is shorter because Swift infers the parameter list and provides $0 automatically.

Swift can do this because the surrounding API already tells the compiler what kind of closure is expected. The shorthand names are part of that inference story, not a separate variable system.

When Shorthand Works Well

Shorthand arguments are best when:

  • the closure body is short
  • the parameter meaning is obvious
  • there are only one or two inputs

Collection helpers are a common example:

swift
let lengths = names.map { $0.count }
let longNames = names.filter { $0.count > 3 }
let combined = zip([1, 2], [10, 20]).map { $0 + $1 }

In those cases, the shorthand reads naturally once you know the rule.

When Explicit Names Are Better

The shorthand is optional. If the closure is complex, explicit names are usually easier to read:

swift
1let sortedPeople = names.sorted { left, right in
2    if left.count == right.count {
3        return left < right
4    }
5    return left.count < right.count
6}

Using $0 and $1 here would work, but it would make the logic harder to follow. Clear parameter names often matter more than saving a few characters.

Another important point is that $0 and $1 do not come from surrounding scope. They are generated by Swift only for that closure's implicit parameters.

Common Pitfalls

One common mistake is thinking $0 is always the first element of an array. It is not. It is simply the first argument passed into the current closure.

Another issue is overusing shorthand in long or nested closures. Code that was concise at first can become much harder to understand once several anonymous parameters are involved.

It is also easy to forget how many arguments a closure receives. Some APIs pass one value, some pass two, and others pass none. The closure signature decides whether $0, $1, or additional shorthand names even exist.

Summary

  • '$0, $1, and similar names are shorthand closure parameters in Swift.'
  • '$0 refers to the first argument, $1 to the second, and so on.'
  • They are most useful in short, obvious closures such as map, filter, and sorted.
  • Explicit parameter names are often better for multi-line or more complex logic.
  • The shorthand names belong only to the current closure and are based on that closure's parameter list.

Course illustration
Course illustration

All Rights Reserved.