Functional Swift: Closures { }

Demystifying @escaping, @non-escaping, @autoclosure and curry function

Aaina jain
Jun 27, 2018 · 7 min read
Credits: Pexels

What’s Function?

Function take 2 params and return 1 param

Function Types

var mathFunction: (Int, Int) -> Int = add

Closure Expression:

Swift Closure Document

Shorthand Argument Names

After specifying shorthand names no need to specify closure arguments and in keyword

Implicit Returns from Closure:

Trailing Closure:

Closure as an argument to method call
Trailing Closure (i.e. closure after method parentheses)
Trailing closure example using reduce()

Capturing Values:

Example given in Swift document

Non-escaping Closures:

The Least You Need to Know

Escaping Closures:

Common Error:

Autoclosures:

func assert(_ expression: @autoclosure () -> Bool,
            _ message: @autoclosure () -> String) {}

Closures vs Blocks:

Closures vs Delegates:

References:


Conclusion:

Swift India

Swift Developer Community in India 🇮🇳

Aaina jain

Written by

Product Engineer📱at Go-Jek Tech, Organizer & Editor at Swift India

Swift India

Swift Developer Community in India 🇮🇳