automatic ref counting - Swift weak self function retention -
if have closure references weak var weakself = self
, can change closure direct function reference, through weakself
?
struct closureholder { let closure: () -> void } class closuresource { func hello() { nslog("hello") } func createweakselfwithininnerclosureclosureholder() -> closureholder { weak var weakself = self return closureholder(closure: { weakself?.hello() }) } func createweakselfdothelloclosureholder() -> closureholder { weak var weakself = self // code below won't compile because weakself optional. // once unwrap optional, no longer have weak reference. // return closureholder(closure: weakself.hello) // strongifies weak reference. :( return closureholder(closure: weakself!.hello) } }
instead of createweakselfwithininnerclosureclosureholder
, i'd prefer createweakselfdothelloclosureholder
.
no can't. saying self.foo
(if foo
method) same thing saying myclass.foo(self)
. , methods curried in fashion keep strong reference receiver object. if want maintain weak reference, need stick { weakself?.hello() }
approach.
Comments
Post a Comment