Does autoclosure respect weak'ness of a captured variable?

I'm confused whether in the following example the weakInstance autoclosure captures self weakly and can therefore return nil:

func perform(weakInstance: @autoclosure () -> SomeType?) {
     weakInstance() // <- Can this method return nil?

someSource.setEventHandler { [weak self] in 
    perform(weakInstance: self)

Is there some technique that would allow to verify in the debugger that self is indeed captured weakly, which can be used in this case and others where exact semantics is not trivial?

Yes. Whenever your event handler is called some time in the future, if self is no longer in memory, perform will be called with nil. You can easily verify it by calling perform using e.g. DispatchQueue.after to delay the call, and deallocate self immediately after.

Terms of Service

Privacy Policy

Cookie Policy