Skip to content

Instantly share code, notes, and snippets.

@nkukushkin
Last active June 13, 2018 16:07
Show Gist options
  • Save nkukushkin/cdba791333fcdaf47da2 to your computer and use it in GitHub Desktop.
Save nkukushkin/cdba791333fcdaf47da2 to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
func doSomething() {}
func howDoesThisWork() {
// Nested function that references `self` implicitly.
func performDoSomething() {
doSomething()
}
dispatch_async(dispatch_get_main_queue()) { [weak self] in
// When we call `doSomething()` compiler asks us to explicitly add `self` to the call.
self?.doSomething()
// However, when we call the nested function it doesn't.
// And the behaviour is ambiguous (probably retains `self`).
performDoSomething()
}
}
}
@nkukushkin
Copy link
Author

https://twitter.com/jckarter/status/697100492288057345

Joe Groff
Yeah, that's a known bug. Capturing the local function will capture self strongly, even from a [weak self] closure.

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