简体   繁体   English

Swift 3转换

[英]Swift 3 Conversion

I have written a code for concurrent API calls in swift 2.2. 我已经在Swift 2.2中编写了用于并发API调用的代码。 When I change from swift 2.2 to swift 3, I am facing issue with syntax of swift. 当我从swift 2.2更改为swift 3时,我面临的问题是swift的语法。 Help me out 帮帮我

    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")
        }
    }

Do you still remember dispatch_apply() . 您是否还记得dispatch_apply() Well, it's still there and got a new name. 好吧,它仍然在那里并获得了一个新名称。 From now on you have to call concurrentPerform() 从现在开始,您必须调用parallelPerform()

 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")
        }
    }

for more information see this 有关更多信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM