Created
June 8, 2016 09:13
-
-
Save krzysztofzablocki/51aa2c1e341c78cb14519bb5437b6edf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UIKit | |
import XCPlayground | |
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
func perform(initial: Int) { | |
var value: Int = initial | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1), dispatch_get_main_queue()) { [myConstant = value] in | |
print("constant in first block \(myConstant)") | |
print("captured \(value)") | |
value = initial * 2 | |
} | |
value = initial + 1 | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2), dispatch_get_main_queue()) { [myConstant = value] in | |
print("constant in second block \(myConstant)") | |
print("captured \(value)") | |
} | |
} | |
perform(10) | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3), dispatch_get_main_queue()) { | |
print("--------") | |
perform(30) | |
} | |
// Console: | |
constant in first block 10 | |
captured 11 | |
constant in second block 11 | |
captured 20 | |
-------- | |
constant in first block 30 | |
captured 31 | |
constant in second block 31 | |
captured 60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment