<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif"> [weak self] and [unowned self] are used to solve the problem of <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html">Strong Reference Cycles Between Class Instances</a>. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">If you can grantee self won&#39;t be nil during the life time, you should use `unowned self` instead of `weak self`. If you can&#39;t grantee that, you should use `weak self` and `<span style="font-size:12.800000190734863px;font-family:arial,sans-serif">return self?.myparam ?? 42`. I don&#39;t think using `</span><span style="font-size:12.800000190734863px;font-family:arial,sans-serif"> if self != nil { return self!.myparam }` is a good idea. The reason is said by you already.</span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px">Zhaoxin</span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 1, 2017 at 11:42 PM, Dennis Weissmann via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On May 1, 2017, at 5:32 PM, Rien &lt;<a href="mailto:Rien@Balancingrock.nl" target="_blank">Rien@Balancingrock.nl</a>&gt; wrote:</div><br class="m_3078716330430626682Apple-interchange-newline"><div><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br class="m_3078716330430626682Apple-interchange-newline">On 01 May 2017, at 16:59, Dennis Weissmann &lt;<a href="mailto:dennis@dennisweissmann.me" target="_blank">dennis@dennisweissmann.me</a>&gt; wrote:<br><br><blockquote type="cite"><br>On May 1, 2017, at 4:46 PM, Rien via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br><br>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.<br><br>Now I am wondering: how does the compiler know that [weak self] is referenced?<br><br>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.<br><br>But when a job is executing it has no control over the exclusive rights to [weak self].<br><br>I.e. [weak self] may be nilled by an outside event in the middle of say:<br><br><span class="m_3078716330430626682Apple-tab-span" style="white-space:pre-wrap">        </span>if self != nil { return self!.myparam }<br><br>The if finding [weak self] non nil, but the return finding [weak self] as nil<br><br>Is that correct? i.e. should we never use the above if construct but always:<br><br><span class="m_3078716330430626682Apple-tab-span" style="white-space:pre-wrap">        </span>return self?.myparam ?? 42<br><br></blockquote><br>Yes, as you said, you never know when self will be nilled out, that&#39;s why you need to create a (temporary) strong reference to it, work on it, and when the block returns, your strong reference is released and self either goes away immediately (incase it was released elsewhere after the local binding to a strong variable and no other objects had a strong reference to it) or it will stay as long as no other object holds a strong reference to it.<br><br>When the closure is more involved than a view lines, I often do a guard let `self` = self else { return } to conveniently work with a strong self inside the rest of the closure.<br></blockquote><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">I was aware of that practise (use it myself) but I was not sure if it would always work.</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">I.e. I have not found it documented that</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span class="m_3078716330430626682Apple-tab-span" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:pre-wrap;word-spacing:0px">        </span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">let strongSelf = self</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">will actually retain ‘self’ an not create a strong reference to an intermediate reference to self.</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">It makes sense though, otherwise there would be massive failures ‘out there’. ;-)</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"></div></blockquote><div><br></div></span><div>Yes :) local variables (as others too) are strong by default. If you want them weak (you should very rarely need that though) you have to explicitly mark them weak.</div><span class=""><div><br></div><div><blockquote type="cite"><span style="float:none;display:inline!important">will actually retain ‘self’ an not create a strong reference to an intermediate reference to self.</span><br></blockquote></div><div><br></div></span><div>Isn&#39;t that the same? I might misunderstand you but they would both reference the same object, no? So in the end the retain message would be sent to the same object, wouldn&#39;t it? </div><span class=""><br><blockquote type="cite"><div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Thanks,</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Rien.</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><blockquote type="cite">Regards,<br>Rien<br><br>Site:<span class="m_3078716330430626682Apple-converted-space"> </span><a href="http://balancingrock.nl/" target="_blank">http://balancingrock.nl</a><br>Blog:<span class="m_3078716330430626682Apple-converted-space"> </span><a href="http://swiftrien.blogspot.com/" target="_blank">http://swiftrien.<wbr>blogspot.com</a><br>Github:<span class="m_3078716330430626682Apple-converted-space"> </span><a href="http://github.com/Balancingrock" target="_blank">http://github.com/<wbr>Balancingrock</a><br>Project:<span class="m_3078716330430626682Apple-converted-space"> </span><a href="http://swiftfire.nl/" target="_blank">http://swiftfire.nl</a><span class="m_3078716330430626682Apple-converted-space"> </span>- A server for websites build in Swift<br><br><br><br><br><br><br>______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></blockquote><br>- Dennis</blockquote></div></blockquote><br></span></div><div>- Dennis</div><br></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>