<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can try container?.forEach(), like</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">let</span><span style="font-variant-ligatures: no-common-ligatures;"> bb:[</span><span style="color:rgb(112,61,170)">String</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures: no-common-ligatures;">]? = [</span><span style="color:rgb(209,47,27)">&quot;aa&quot;</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color:rgb(39,42,216)">1</span><span style="font-variant-ligatures: no-common-ligatures;">, </span><span style="color:rgb(209,47,27)">&quot;bb&quot;</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color:rgb(39,42,216)">2</span><span style="font-variant-ligatures: no-common-ligatures;">, </span><span style="color:rgb(209,47,27)">&quot;cc&quot;</span><span style="font-variant-ligatures: no-common-ligatures;">:</span><span style="color:rgb(39,42,216)">3</span><span style="font-variant-ligatures: no-common-ligatures;">]</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(79,129,135)">bb</span><span style="color:rgb(0,0,0)">?.</span><span style="font-variant-ligatures: no-common-ligatures;">forEach</span><span style="color:rgb(0,0,0)"> { </span><span style="font-variant-ligatures: no-common-ligatures;">print</span><span style="color:rgb(0,0,0)">($0) }</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;">/* </span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;">(&quot;aa&quot;, 1)</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;">(&quot;bb&quot;, 2)</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;">(&quot;cc&quot;, 3)</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)">


</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;">*/</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">Zhaoxin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 29, 2016 at 6:14 AM, Saagar Jha 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"><div>The nil check and creating an empty array have very similar performance, in my naïve testing. </div><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">Saagar Jha<br><br><br></div>

</div><div><div class="h5">
<br><div><blockquote type="cite"><div>On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div>You should test it out — I&#39;d guess there&#39;s a good chance it gets optimized out.<br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 28, 2016 at 2:58 PM Rick Mann &lt;<a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a>&gt; wrote:<br></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>
<br>
I suppose there&#39;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&#39;s unlikely to cause issues in practice.<br>
<br>
Thanks.<br>
<br>
&gt; On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" target="_blank">jtbandes@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; How about &quot;for item in someOptionalContainer ?? []&quot;  ?<br>
&gt;<br>
&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">swift-users@swift.org</a>&gt; wrote:<br>
&gt; I often call methods that return an optional collection. I then iterate over it. The problem is, it&#39;s a bit cumbersome to write:<br>
&gt;<br>
&gt;      if let container = someOptionalContainer<br>
&gt;     {<br>
&gt;         for item in container<br>
&gt;         {<br>
&gt;         }<br>
&gt;     }<br>
&gt;<br>
&gt; I wish I could just write<br>
&gt;<br>
&gt;     for item in someOptionalContainer<br>
&gt;     {<br>
&gt;     }<br>
&gt;<br>
&gt; such that if the optional is nil, it just skips the iteration altogether.<br>
&gt;<br>
&gt; Is there a syntax for that (especially in Swift 3)?<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Rick Mann<br>
&gt; <a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a><br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
&gt;<br>
<br>
<br>
--<br>
Rick Mann<br>
<a href="mailto:rmann@latencyzero.com" target="_blank">rmann@latencyzero.com</a><br>
<br>
<br>
</blockquote></div>
_______________________________________________<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/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div><br>_______________________________________________<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/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>