Syntax

Swift Closure Syntax

In Swift, closure: writes an inline function — let doubled = nums.map { $0 * 2 }

Quick Answer

Syntax
let doubled = nums.map { $0 * 2 }
SwiftSyntax

Closure

Code example
let doubled = nums.map { $0 * 2 }

Writes an inline function.

Learn More

How to closure in Swift

  1. Open your Swift file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Swift Closure Syntax

Use let doubled = nums.map { $0 * 2 }.

Related Syntax References

Frequently asked questions

What is the Swift syntax for closure?

Use this pattern: let doubled = nums.map { $0 * 2 }

Can I copy this Swift code example?

Yes. Use Copy code and adapt the example to your program.

Is this Swift syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify