I would do something like:
import Foundation
class Task { // Must be a class to prevent Swift copy semantics
eliminating the result
private static let queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_HIGH, 0)
private let group = dispatch_group_create()
private var _result: String?
init(name: String) {
dispatch_group_async(group, Task.queue) {
self._result = name // The asynchronous task!
}
}
var result: String { // Provide safe access to result
dispatch_group_wait(group, DISPATCH_TIME_FOREVER) // Block
until task finished
return _result!
}
}
var tasks = [String : Task]()
let names = ["One", "Two"]
names.forEach {
tasks[$0] = Task(name: $0)
}
tasks.map { (_, task) in // Prints [One, Two] in playground
task.result
}
On 11 December 2015 at 07:02, Dmitri Gribenko via swift-users <
[email protected]> wrote:
> On Thu, Dec 10, 2015 at 11:20 AM, Jens Alfke <[email protected]> wrote:
> > All we’re saying is that a class like this isn’t commonly useful enough
> to go into a library.
>
> And too easy to misuse if provided.
>
> Dmitri
>
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <[email protected]>*/
> _______________________________________________
> swift-users mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-users
>
--
-- Howard.
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users