<div dir="ltr"><font face="arial, helvetica, sans-serif">I would do something like:</font><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> <span class="">import</span><span class=""> Foundation</span></font></div>
<p class=""><font face="monospace, monospace"><span class=""> class</span><span class=""> Task { </span><span class="">// Must be a class to prevent Swift copy semantics eliminating the result</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">private</span><span class=""> </span><span class="">static</span><span class=""> </span><span class="">let</span><span class=""> queue = </span><span class="">dispatch_get_global_queue</span><span class="">(</span><span class="">DISPATCH_QUEUE_PRIORITY_HIGH</span><span class="">, </span><span class="">0</span><span class="">)</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">private</span><span class=""> </span><span class="">let</span><span class=""> group = </span><span class="">dispatch_group_create</span><span class="">()</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">private</span><span class=""> </span><span class="">var</span><span class=""> _result: </span><span class="">String</span><span class="">?</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">init</span><span class="">(name: </span><span class="">String</span><span class="">) {</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">dispatch_group_async</span><span class="">(</span><span class="">group</span><span class="">, </span><span class="">Task</span><span class="">.queue) {</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">self</span><span class="">.</span><span class="">_result</span><span class=""> = name </span><span class="">// The asynchronous task!</span></font></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">var</span><span class=""> result: </span><span class="">String</span><span class=""> { </span><span class="">// Provide safe access to result</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">dispatch_group_wait</span><span class="">(</span><span class="">group</span><span class="">, </span><span class="">DISPATCH_TIME_FOREVER</span><span class="">) </span><span class="">// Block until task finished</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">return</span><span class=""> </span><span class="">_result</span><span class="">!</span></font></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p>
<p class=""><font face="monospace, monospace"><span class=""></span><br></font></p>
<p class=""><font face="monospace, monospace"><span class=""> var</span><span class=""> tasks = [</span><span class="">String</span><span class=""> : </span><span class="">Task</span><span class="">]()</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> let</span><span class=""> names = [</span><span class="">"One"</span><span class="">, </span><span class="">"Two"</span><span class="">]</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> names</span><span class="">.</span><span class="">forEach</span><span class=""> {</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> </span><span class="">tasks</span><span class="">[$0] = </span><span class="">Task</span><span class="">(name: $0)</span></font></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p>
<p class=""><font face="monospace, monospace"><span class=""> tasks</span><span class="">.</span><span class="">map</span><span class=""> { (</span><span class="">_</span><span class="">, task) </span><span class="">in // Prints [One, Two] in playground</span></font></p>
<p class=""><font face="monospace, monospace"><span class=""> task.</span><span class="">result</span></font></p>
<p class=""><span class=""><font face="monospace, monospace"> }</font></span></p><div class="gmail_extra"><font face="monospace, monospace"><br></font><div class="gmail_quote"><font face="monospace, monospace">On 11 December 2015 at 07:02, Dmitri Gribenko via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br></font><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><font face="monospace, monospace">On Thu, Dec 10, 2015 at 11:20 AM, Jens Alfke <<a href="mailto:jens@mooseyard.com">jens@mooseyard.com</a>> wrote:<br>
> All we’re saying is that a class like this isn’t commonly useful enough to go into a library.<br>
<br>
And too easy to misuse if provided.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>>*/<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>
</font></span></font></blockquote></div><font face="monospace, monospace"><br><br clear="all"></font><div><font face="monospace, monospace"><br></font></div><font face="monospace, monospace">-- <br></font><div class="gmail_signature"><font face="monospace, monospace"> -- Howard.</font><br></div>
</div></div>