Monday, July 25, 2016

Swift Closure Capture Semantics

Olivier Halligon:

One important thing to note though is that in Swift the captured variables are evaluated at the closure execution’s time. We could say that it captures the reference (or pointer) to the variable.

For those of you who know Objective-C, you can notice that the Swift behavior is unlike Objective-C’s default block semantics but instead somewhat like if the variable had the __block modifier in Objective-C.

[…]

To capture the value of a variable at the point of the closure’s creation (instead of a reference to the variable itself), you can use the [localVar = varToCapture] capture list.

Comments RSS · Twitter

Leave a Comment