Skip to content
tezvyn:
Menu

Closures and trailing closure syntax

MediumHow cards are made

Summary

first-class functions and capture.

Key points

closures are self-contained function blocks that capture surrounding state; a final closure parameter can be passed as a trailing closure outside the parentheses.

What's really being asked

It checks core Swift fluency: whether you understand closures as first-class values that capture context and know the syntactic sugar that makes APIs read cleanly.

What closures are

A closure is a self-contained block of functionality you can pass around and call later. Closures are reference types and they capture, by reference, the constants and variables from the surrounding scope they use, keeping that state alive as needed. Global functions and nested functions are special cases of closures. Because they are first-class, you can store them in variables, pass them as parameters, and return them.

Trailing closure syntax

When a function's final parameter is a closure, Swift lets you write that closure after the call's closing parenthesis instead of inside it, which reads much better for blocks of code. If the closure is the only argument, you can omit the parentheses altogether. Multiple trailing closures are also supported, with the first unlabeled and the rest labeled.

A simple example

Define a function func perform(times: Int, action: () -> Void). Calling it as perform(times: 3) { print("hi") } uses trailing closure syntax: the action block sits outside the parentheses. The function would loop times times and invoke action each pass.

The mistakes people make

Confusing trailing closure syntax with escaping, which is about whether the closure outlives the call, or with autoclosure, which wraps an expression automatically. Saying closures copy captured values; they capture references by default. Forgetting that the closure must be the final parameter to use the unparenthesized form.

What usually comes next

What is an escaping closure? How does capture differ from copying? What is a capture list for? How do multiple trailing closures look at a call site?

A concrete example

A networking helper exposes func fetch(url: URL, completion: (Data?) -> Void). At the call site you write fetch(url: endpoint) { data in handle(data) }, passing the completion as a trailing closure so the handling code reads as a natural block after the call rather than being crammed inside the argument list.

Interview question

Trailing closure syntax is permitted under which condition?

  • a.Only when the closure is marked @escaping
  • b.When the closure is the function's final parameter, allowing it to be written after the parenthesesCorrect
  • c.Only when the function returns Void
  • d.Only for closures that take no arguments
Why?

Trailing closure syntax applies when the closure is the last parameter. Escaping, argument count, and return type are unrelated to whether the sugar is allowed.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.

See open roles