Swift 3转换

我用swift 2.2编写了一个并发API调用的代码。 当我从swift 2.2更改为swift 3时,我正在用swift的语法来解决问题。 帮帮我

let endPoints = [.email, .others] let fetchGroup = dispatch_group_create() let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_apply(endPoints.count, queue) { (index) in let enumType = endPoints[index] switch enumType { case .email: //Make email api call break case .others: //Make other api break default: break } } dispatch_group_notify(fetchGroup, dispatch_get_main_queue()) { if endPoints.count > 0 { fail("error") } } 

你还记得dispatch_apply()吗? 那么,它还在那里,并得到一个新的名字。 从现在起,你必须调用concurrentPerform()

  let endPoints = [.email, .others] let fetchGroup = DispatchGroup() let queue = DispatchQueue.global (qos : .default) DispatchQueue.concurrentPerform(iterations: endPoints.count) { (index) in let enumType = endPoints[index] switch enumType { case .email: //Make email api call break case .others: //Make other api break default: break } } DispatchGroup().notify(queue: DispatchQueue.main) { if endPoints.count > 0 { fail("error") } } 

欲了解更多信息,请参阅