【发布时间】:2018-01-24 06:24:40
【问题描述】:
我创建的函数有问题,我想返回多个值,但似乎一直收到此错误
void 函数中出现意外的非 void 返回值
func calculateDistance(_ firstLat: Double, _ firstLong: Double, _ secondLat: Double, _ secondLong: Double) -> (Double, Double, String) {
let URL = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(firstLat),\(firstLong)&destinations=\(secondLat),\(secondLong)&key=KEYID"
Alamofire.request(URL)
.responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
let distance = json["rows"][0]["elements"][0]["distance"]["value"].double! / 1000 // Double
let price = distance * 1.0 // Double
let duration = json["rows"][0]["elements"][0]["duration"]["text"] // String
return (distance, price, duration) // Keep getting this error on this line
}
}
}
我做错了什么?我返回了正确的数据类型。
【问题讨论】:
-
我认为您需要使用完成处理程序。
-
使用完成处理程序,不返回,你也没有在当前函数上返回任何东西,
Alamorefire.request在闭包中不返回任何东西