Skip to content

Instantly share code, notes, and snippets.

@zhangxigithub
Created January 3, 2018 15:24
Show Gist options
  • Save zhangxigithub/d96735729d2bd3f94651a349fd3152f0 to your computer and use it in GitHub Desktop.
Save zhangxigithub/d96735729d2bd3f94651a349fd3152f0 to your computer and use it in GitHub Desktop.
Swift closure capture or not
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