Skip to content

Instantly share code, notes, and snippets.

@Thomvis
Last active April 13, 2021 12:13
Show Gist options
  • Save Thomvis/97212c65ce84541f63f2a1e3e903c407 to your computer and use it in GitHub Desktop.
Save Thomvis/97212c65ce84541f63f2a1e3e903c407 to your computer and use it in GitHub Desktop.
Capturing a weak reference to self in a nested closure could cause a retain cycle. `[weak self]` weakly captures self from the outer closure. Since the outer closure did not declare a weak reference to self, it will keep a strong one.
class ViewModel {
let intGetterGetter: () -> () -> Int?
var currentInt = 2
init() {
self.intGetterGetter = {
return { [weak self] in
return self?.currentInt
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment