Created
January 3, 2018 15:24
-
-
Save zhangxigithub/d96735729d2bd3f94651a349fd3152f0 to your computer and use it in GitHub Desktop.
Swift closure capture or not
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
var a = 1 | |
var b = 1 | |
//Do not capture | |
let add1 = { | |
print(a+b) | |
} | |
add1() //2 | |
a = 2 | |
b = 3 | |
add1() //5 | |
//capture | |
let add2 = {[a,b] in | |
print(a+b) | |
} | |
add2() //5 | |
a = 3 | |
b = 4 | |
add2() //is 5, not 7. add2 capture a and b's value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment