[swift-users] weak self

Rien Rien at Balancingrock.nl
Mon May 1 09:46:12 CDT 2017


In my code I use a lot of queues. And (very often) I will use [weak self] to prevent doing things when ‘self’ is no longer available.

Now I am wondering: how does the compiler know that [weak self] is referenced?

I am assuming it keeps a reverse reference from self to the [weak self] in order to ‘nil’ the [weak self] when self is nilled.

But when a job is executing it has no control over the exclusive rights to [weak self].

I.e. [weak self] may be nilled by an outside event in the middle of say:

	if self != nil { return self!.myparam }

The if finding [weak self] non nil, but the return finding [weak self] as nil

Is that correct? i.e. should we never use the above if construct but always:

	return self?.myparam ?? 42

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift








More information about the swift-users mailing list