Last active
June 12, 2016 11:15
-
-
Save AdrianFerreyra/7c79225577b5d90c7bcb78e0447f9281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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