Skip to content

Instantly share code, notes, and snippets.

@qnoid
Created January 22, 2016 12:31
Show Gist options
  • Save qnoid/7008c4d7abc226d99913 to your computer and use it in GitHub Desktop.
Save qnoid/7008c4d7abc226d99913 to your computer and use it in GitHub Desktop.
In a capture list, there is no need to capture a reference to "self" if you need a reference to an instance self holds. https://twitter.com/qnoid/status/690507191057551360
class ViewController : UIViewController
{
var foo = "foo"
func hello()
{
let closure = { [foo = self.foo] in
print("Hello \(foo)!")
}
}
}
@qnoid
Copy link
Author

qnoid commented Jan 22, 2016

“Capture Lists
By default, a closure expression captures constants and variables from its surrounding scope with strong references to those values. You can use a capture list to explicitly control how values are captured in a closure.”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.1).” iBooks. https://itun.es/gb/jEUH0.l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment