Skip to content

Instantly share code, notes, and snippets.

@AdrianFerreyra
Last active June 12, 2016 11:15
Show Gist options
  • Save AdrianFerreyra/7c79225577b5d90c7bcb78e0447f9281 to your computer and use it in GitHub Desktop.
Save AdrianFerreyra/7c79225577b5d90c7bcb78e0447f9281 to your computer and use it in GitHub Desktop.
import Foundation
var a = 0
let closure = {
return a
}
a = 1
closure.dynamicType
closure() //returns 1. Captures a reference to the variable.
func innerFunction() -> Int {
var a = 0
func inner() -> Int {
return a
}
a = 1
return inner()
}
innerFunction.dynamicType //Dynamic Type matches
innerFunction() //returns 1. Captures a reference to the variable.
//It's exactly the same as the previous closure.
let valueCapturingClosure = {[a] in
return a
}
a = 2
valueCapturingClosure.dynamicType //Dynamic Type matches
valueCapturingClosure() //returns 1. Captures the value, not the reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment