<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="">Like Rick, I had also wondered about a simple way to do this. Perhaps this is a question for swift-evolution, but wouldn’t it be desirable if Swift supported:<div class=""><br class=""></div><div class=""> for item in someOptionalContainer?<br class=""> {</div><div class=""> }<br class=""><div class=""><br class=""></div><div class="">which seems more natural and intuitive that the alternatives that have been suggested.</div><div class=""><br class=""></div><div class="">Kerry</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 28, 2016, at 5:18 PM, Zhao Xin via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><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 class=""></div><div class="gmail_default"><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> bb:[</span><span style="color:rgb(112,61,170)" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span><span style="color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">]? = [</span><span style="color:rgb(209,47,27)" class="">"aa"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span><span style="color:rgb(39,42,216)" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="color:rgb(209,47,27)" class="">"bb"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span><span style="color:rgb(39,42,216)" class="">2</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, </span><span style="color:rgb(209,47,27)" class="">"cc"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span><span style="color:rgb(39,42,216)" class="">3</span><span style="font-variant-ligatures: no-common-ligatures;" class="">]</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(61, 29, 129);" class=""><span style="color:rgb(79,129,135)" class="">bb</span><span style="" class="">?.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">forEach</span><span style="" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures;" class="">print</span><span style="" class="">($0) }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">/* </span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">("aa", 1)</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">("bb", 2)</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">("cc", 3)</span></div><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(61,29,129)" class=""><span style="" class="">
</span></p><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">*/</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Zhaoxin</font></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Jul 29, 2016 at 6:14 AM, Saagar Jha via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">The nil check and creating an empty array have very similar performance, in my naïve testing. </div><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class="">Saagar Jha<br class=""><br class=""><br class=""></div>
</div><div class=""><div class="h5">
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:</div><br class=""><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 <<a href="mailto:rmann@latencyzero.com" target="_blank" class="">rmann@latencyzero.com</a>> 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="">
> On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" target="_blank" class="">jtbandes@gmail.com</a>> wrote:<br class="">
><br class="">
> How about "for item in someOptionalContainer ?? []" ?<br class="">
><br class="">
> On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:<br class="">
> 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="">
><br class="">
> if let container = someOptionalContainer<br class="">
> {<br class="">
> for item in container<br class="">
> {<br class="">
> }<br class="">
> }<br class="">
><br class="">
> I wish I could just write<br class="">
><br class="">
> for item in someOptionalContainer<br class="">
> {<br class="">
> }<br class="">
><br class="">
> such that if the optional is nil, it just skips the iteration altogether.<br class="">
><br class="">
> Is there a syntax for that (especially in Swift 3)?<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="">
> _______________________________________________<br class="">
> swift-users mailing list<br class="">
> <a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
> <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="">
><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" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div><br class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<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="">
<br class=""></blockquote></div><br class=""></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=""></div></div></body></html>