gpt4 book ai didi

ios - swift 3 转换

转载 作者:可可西里 更新时间:2023-11-01 00:58:24 24 4
gpt4 key购买 nike

我已经为 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")
}
}

有关详细信息,请参阅 this

关于ios - swift 3 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com