Closures

Overview

  • Closures are the single toughest thing to learn in Swift (HWS).

  • Closure is lean version of Function

  • Closure can be used in Function

  • Services like Siri and Network Connections use Closures in an app to make queries faster. (HWS)

How closures return a value

Here Closure returns nothing

let payment = { (user: String) in
    print("Paying \(user)…")
}

Here Closure returns value

let payment = { (user: String) -> Bool in
    print("Paying \(user)…")
    return true
}

Functions vs. Closures

Trailing Closure

If a function's final parameters are functions, use trailing closure syntax (HWS).

Shorthand Parameters

Use it in very specific conditions.

Sample Codes

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Source: https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure

Sources

Videos

Articles / Documents

Last updated

Was this helpful?