iOS swift如何在需要返回值的函数内等待异步任务

Pab*_*rez 0 asynchronous ios swift nsurlsessiondatatask swift3

我正在使用swift 3.0并创建了一个返回整数数组的函数.整数数组非常具体,它们来自数据库,因此HTTP调用是异步的.这是一个功能,因为我在3个不同的控制器中使用它,所以写一次是有意义的.我的问题是在底部的return语句之后返回异步代码,因此返回nil.我在这里试过这个例子等待任务完成但是它不起作用主要是因为我需要返回值.这是我的代码

func ColorSwitch(label: [UILabel]) -> [Int] {

    for (index, _) in label.enumerated() {
       label[index].isHidden = true
    }

    // I need the value of this variable in the return
    // statement after the async is done
    var placeArea_id = [Int]()

    let urll:URL = URL(string:ConnectionString+"url")!

    let sessionn = URLSession.shared
    var requestt = URLRequest(url: urll)
    requestt.httpMethod = "POST"


    let group = DispatchGroup()
    group.enter()

    let parameterr = "http parameters"
    requestt.httpBody = parameterr.data(using: String.Encoding.utf8)
    let task =   sessionn.dataTask(with:requestt, completionHandler: {(data, response, error) in
        if error != nil {
            print("check check error")
        } else {
            do {

                let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
                DispatchQueue.main.async {

                    if let Profiles = parsedData?["Results"] as? [AnyObject] {
                        if placeArea_id.count >= 0 {
                            placeArea_id = [Int]()   
                        }

                        for Profiles in Profiles {

                            if let pictureS = Profiles["id"] as? Int {
                                placeArea_id.append(pictureS)
                            }
                        }
                    }
                    group.leave()
                }

            } catch let error as NSError {
                print(error)

            }
        }
    })
    task.resume()


    group.notify(queue: .main) {
   // This is getting the value however can't return it here since it 
   // expects type Void
    print(placeArea_id)

    }
   // this is nil
    return placeArea_id

}
Run Code Online (Sandbox Code Playgroud)

我已经检查过,并且值在异步代码中返回,现在只需要返回它,任何建议都会很棒.

Asl*_*ace 5

您将需要使用闭包,或将您的函数更改为同步.

func ColorSwitch(label: [UILabel], completion:@escaping ([Int])->Void) {
    completion([1,2,3,4]) // when you want to return
}

ColorSwitch(label: [UILabel()]) { (output) in
    // output is the array of ints
    print("output: \(output)")
}
Run Code Online (Sandbox Code Playgroud)

这是关于闭包的一个非常好的博客http://goshdarnclosuresyntax.com/