<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. </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 <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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 <<a href="mailto:rmann@latencyzero.com" 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" 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>