<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">The nil check and creating an empty array have very similar performance, in my naïve testing.&nbsp;</div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Saagar Jha<br class=""><br class=""><br class=""></div>

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">You should test it out — I'd guess there's a good chance it gets optimized out.<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Jul 28, 2016 at 2:58 PM Rick Mann &lt;<a href="mailto:rmann@latencyzero.com" class="">rmann@latencyzero.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Yeah, I suppose that works. Feels a bit clunky, like the language lacks specific support for this (in that it provides specific support for so many other common constructs). But I guess I can make do with that.<br class="">
<br class="">
I suppose there's a bit of a performance hit, in that constructing an empty array and iterating over it is more expensive than a simple nil check, but that's unlikely to cause issues in practice.<br class="">
<br class="">
Thanks.<br class="">
<br class="">
&gt; On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" target="_blank" class="">jtbandes@gmail.com</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; How about "for item in someOptionalContainer ?? []"&nbsp; ?<br class="">
&gt;<br class="">
&gt; On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt; I often call methods that return an optional collection. I then iterate over it. The problem is, it's a bit cumbersome to write:<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp; if let container = someOptionalContainer<br class="">
&gt;&nbsp; &nbsp; &nbsp;{<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for item in container<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br class="">
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br class="">
&gt;&nbsp; &nbsp; &nbsp;}<br class="">
&gt;<br class="">
&gt; I wish I could just write<br class="">
&gt;<br class="">
&gt;&nbsp; &nbsp; &nbsp;for item in someOptionalContainer<br class="">
&gt;&nbsp; &nbsp; &nbsp;{<br class="">
&gt;&nbsp; &nbsp; &nbsp;}<br class="">
&gt;<br class="">
&gt; such that if the optional is nil, it just skips the iteration altogether.<br class="">
&gt;<br class="">
&gt; Is there a syntax for that (especially in Swift 3)?<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; --<br class="">
&gt; Rick Mann<br class="">
&gt; <a href="mailto:rmann@latencyzero.com" target="_blank" class="">rmann@latencyzero.com</a><br class="">
&gt;<br class="">
&gt;<br class="">
&gt; _______________________________________________<br class="">
&gt; swift-users mailing list<br class="">
&gt; <a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
&gt;<br class="">
<br class="">
<br class="">
--<br class="">
Rick Mann<br class="">
<a href="mailto:rmann@latencyzero.com" target="_blank" class="">rmann@latencyzero.com</a><br class="">
<br class="">
<br class="">
</blockquote></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>