GCD (Grand Central Dispatch)

Performing Asynchronous Operations in iOS

Most important properties for closures are

  1. They are the main first class types of the language

a. Return from functions or closures

b.Assign to variables and constants

c. Receive as parameters of functions and closures

d. Add to Arrays or Dictionaries

2. They capture their lexical environment.

This was confusing so lets grab a playground and play !


// First Class

import UIKit

// assign to var or constant

let f = {(x:Int) -> Int

in

return x + 42}

f(9)

f(76)

let closures = [f,

{(x:Int) -> Int in return x*2},

{x in return x — 8},

{x in x * x},

{$0 * 42}]

for fn in closures{

fn(42)

}