[swift-users] weak self
Adrian Zubarev
adrian.zubarev at devandartist.com
Mon May 1 09:55:03 CDT 2017
Not answering the questions, but sharing a neat trick.
[weak self] in
guard let `self` = self else { return }
self.foo() // strong self :)
--
Adrian Zubarev
Sent with Airmail
Am 1. Mai 2017 um 16:46:33, Rien via swift-users (swift-users at swift.org) schrieb:
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
_______________________________________________
swift-users mailing list
swift-users at swift.org
https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170501/ad7c23c3/attachment.html>
More information about the swift-users
mailing list