Swift - For循环返回nil数组

时间:2016-10-17 19:19:23

标签: ios swift alamofire

我正在尝试创建一个for循环,它会在每次迭代后将数据附加到数组中。我得到" []" (nil)作为我尝试打印数组时的响应。这是我的代码:

    class func fetchChartPoints(days: Int, base: String, target: String, completion:@escaping (_ chartPoints: [Double]) -> ()) {

    let datesArray = formatDate(days: days)
    var array = [Double]()

    for date in datesArray {
        Alamofire.request("http://api.fixer.io/\(date)?base=\(base)").responseJSON { response in

            //print(response)

            if let resultJSON = response.result.value as? [String : AnyObject]  {
                let rates = resultJSON["rates"] as? [String: Double]

                array.append((rates?[target])!)
            }
        }
    }
    completion(array)
    print(array)

}

0 个答案:

没有答案