can non escaping closure capture surrounding context in swift ?

Asked on 08/09/2024

1 search

Yes, non-escaping closures can capture surrounding context in Swift. When a non-escaping closure captures a variable, it captures that variable by reference. This allows changes to the variable to be observed in the original scope and vice versa. Since the closure is non-escaping, it does not change the lifetime of the variable, and the closure can handle this by just capturing a pointer to the variable's allocation.

For more details, you can refer to the session Explore Swift performance (28:29).